-1

嘿,我已经搜索了一段时间,并多次尝试解决 updateUI(); 处的错误。我无法让它工作,应用程序在启动时不断崩溃。代码已发布

“无法开始活动_ __ _ __ _ __ _ ___ java.lang.NullPointerException”

public class MainActivity extends SherlockFragmentActivity {

private TextView txtStatus;
private ImageView imageView;
int i=0;
int imgid[]={R.drawable.new_york_city_1,R.drawable.facebook,R.drawable.groupsms,R.drawable.twittersms };
RefreshHandler refreshHandler=new RefreshHandler();

class RefreshHandler extends Handler{
    @Override
    public void handleMessage(Message msg) {
        // TODO Auto-generated method stub
        MainActivity.this.updateUI();
    }
    public void sleep(long delayMillis){
        this.removeMessages(0);
        sendMessageDelayed(obtainMessage(0), delayMillis);
    }
};
public void updateUI(){
    int currentInt=Integer.parseInt((String)txtStatus.getText())+10;
    if(currentInt<=100){
        refreshHandler.sleep(2000);
        txtStatus.setText(String.valueOf(currentInt));
        if(i<imgid.length){
            imageView.setImageResource(imgid[i]);

            i++;
        }
    }
}@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.drawer_main);

    SpannableString s = new SpannableString("DJUICE");
    s.setSpan(new TypefaceSpan(this, "djuice"), 0, s.length(),
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

    // Update the action bar title with the TypefaceSpan instance
    ActionBar actionBar = getSupportActionBar();
    actionBar.setTitle(s);

    this.txtStatus=(TextView)this.findViewById(R.id.textViewM);
    this.imageView=(ImageView)this.findViewById(R.id.imageViewM);
    updateUI();
}

XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" 
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:paddingTop="4dp"
android:paddingBottom="4dp"
android:background="#FFFFFF">

<TextView android:text="10"
          android:id="@+id/textViewM"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"></TextView>
<ImageView android:id="@+id/imageViewM"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" ></ImageView></RelativeLayout>

日志猫:

E/AndroidRuntime( 4898): FATAL EXCEPTION: main
E/AndroidRuntime( 4898): java.lang.RuntimeException: Unable to start activity
ComponentInfo{runaway.fridge.potohar/runaway.fridge.potohar.MainActivity}: 
java.lang.NullPointerException
E/AndroidRuntime( 4898):    at 
runaway.fridge.potohar.MainActivity.updateUI(MainActivity.java:59)
E/AndroidRuntime( 4898):    at 
runaway.fridge.potohar.MainActivity.onCreate(MainActivity.java:85)
4

3 回答 3

1

猜测一下,您正在调用updateUI()它正在尝试做的事情

Integer.parseInt((String)txtStatus.getText())+10;

在将值放入字段之前。这意味着它试图解析任何内容,并且有你的 NullPointer。

要测试它,只需在txtStatus.setText("10");之前添加updateUI(),看看你是否停止崩溃。

如果是这样,那就是null在获取该输入以停止崩溃时检查。

此外,无需投(String)txtStatus.getText()只是做txtStatus.getText().toString();

于 2013-09-02T23:32:45.807 回答
0

确保您已将活动添加到清单中。通常这会导致这个问题,

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="your.package.name">

<uses-sdk android:minSdkVersion="8" />

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".FirstActivity" android:label="@string/app_name"></activity>
    <activity android:name=".SecondActivity"></activity>
</application>
 </manifest>
于 2013-09-02T23:38:57.143 回答
0

我想也许你想在 updateUI()

int currentInt=Integer.parseInt(txtStatus.getText().toString())+10;
于 2013-09-02T23:34:01.543 回答