嘿,我现在已经搜索了一段时间,并尝试过多次在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>
logcat的:
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)
答案 0 :(得分:1)
猜测,您正在调用updateUI()
正在尝试
Integer.parseInt((String)txtStatus.getText())+10;
。意思是它试图解析任何东西,而且你的NullPointer。
要对其进行测试,只需在txtStatus.setText("10");
之前添加updateUI()
,看看是否停止了崩溃。
如果是这样,只需检查null
获取该输入以停止崩溃。
此外,无需强制(String)txtStatus.getText()
执行txtStatus.getText().toString();
答案 1 :(得分:0)
我想也许你想在updateUI()
中这样做int currentInt=Integer.parseInt(txtStatus.getText().toString())+10;
答案 2 :(得分:0)
确保您已将活动添加到Manifest。通常会导致此问题,
<?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>