我收到致命异常/空指针异常。我不知道为什么
如果需要任何其他信息,请告知我们。 为什么我得到Nullpointer异常? 如果您需要更多信息,请告诉我。
public class MainActivity extends Activity {
private ImageButton car;
private ImageButton foot;
private ImageButton bus;
private ImageButton train;
private ImageButton carlift;
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.startpic);
// display the logo during 5 secondes,
new CountDownTimer(5000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
}
@Override
public void onFinish() {
// set the new Content of your activity
MainActivity.this.setContentView(R.layout.activity_main);
}
}.start();
car = (ImageButton) findViewById(R.id.imageButton1);
foot = (ImageButton) findViewById(R.id.imageButton2);
bus = (ImageButton) findViewById(R.id.imageButton3);
train = (ImageButton) findViewById(R.id.imageButton4);
carlift = (ImageButton) findViewById(R.id.imageButton5);
car.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.car);
}
});
foot.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.foot);
}
});
bus.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.bus);
}
});
train.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.train);
}
});
carlift.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.carlift);
}
});
super.onCreate(savedInstanceState);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
这是我的Logcat
03-11 12:46:22.637: E/AndroidRuntime(1769): FATAL EXCEPTION: main
03-11 12:46:22.637: E/AndroidRuntime(1769): Process: com.gunasiri.home, PID: 1769
03-11 12:46:22.637: E/AndroidRuntime(1769): java.lang.RuntimeException: Unable to start
activity ComponentInfo{com.gunasiri.home/com.gunasiri.home.MainActivity}:
java.lang.NullPointerException
03-11 12:46:22.637: E/AndroidRuntime(1769): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
03-11 12:46:22.637: E/AndroidRuntime(1769): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
03-11 12:46:22.637: E/AndroidRuntime(1769): at android.app.ActivityThread.access$700(ActivityThread.java:135)
03-11 12:46:22.637: E/AndroidRuntime(1769): at android.os.Handler.dispatchMessage(Handler.java:102)
03-11 12:46:22.637: E/AndroidRuntime(1769): at android.os.Looper.loop(Looper.java:137)
03-11 12:46:22.637: E/AndroidRuntime(1769): at android.app.ActivityThread.main(ActivityThread.java:4998)
03-11 12:46:22.637: E/AndroidRuntime(1769): at java.lang.reflect.Method.invokeNative(Native Method)
03-11 12:46:22.637: E/AndroidRuntime(1769): at java.lang.reflect.Method.invoke(Method.java:515)
03-11 12:46:22.637: E/AndroidRuntime(1769): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
03-11 12:46:22.637: E/AndroidRuntime(1769): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
03-11 12:46:22.637: E/AndroidRuntime(1769): at dalvik.system.NativeStart.main(Native Method)
03-11 12:46:22.637: E/AndroidRuntime(1769): Caused by: java.lang.NullPointerException
03-11 12:46:22.637: E/AndroidRuntime(1769): at com.gunasiri.home.MainActivity.onCreate(MainActivity.java:39)
03-11 12:46:22.637: E/AndroidRuntime(1769): at android.app.Activity.performCreate(Activity.java:5243)
03-11 12:46:22.637: E/AndroidRuntime(1769): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-11 12:46:22.637: E/AndroidRuntime(1769): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
03-11 12:46:22.637: E/AndroidRuntime(1769): ... 11 more
答案 0 :(得分:1)
ImageButtons现在都是null
,因为尚未设置正确的内容视图。您需要在OnClickListener
方法中设置onFinish()
。
所以你的代码是:
public class MainActivity extends Activity {
private ImageButton car;
private ImageButton foot;
private ImageButton bus;
private ImageButton train;
private ImageButton carlift;
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.startpic);
// display the logo during 5 secondes,
new CountDownTimer(5000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
}
@Override
public void onFinish() {
// set the new Content of your activity
MainActivity.this.setContentView(R.layout.activity_main);
car = (ImageButton) findViewById(R.id.imageButton1);
foot = (ImageButton) findViewById(R.id.imageButton2);
bus = (ImageButton) findViewById(R.id.imageButton3);
train = (ImageButton) findViewById(R.id.imageButton4);
carlift = (ImageButton) findViewById(R.id.imageButton5);
car.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.car);
}
});
foot.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.foot);
}
});
bus.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.bus);
}
});
train.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.train);
}
});
carlift.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.carlift);
}
});
}
}.start();
super.onCreate(savedInstanceState);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}