嘿,我已经搜索了一段时间,并多次尝试解决 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)