<标题> LOGCAT h1> div class="one_answers">
我得到一个致命的异常/空指针异常。我不知道为什么
如果需要任何额外的信息,请让我知道。为什么我得到一个空指针异常?如果你需要更多的信息,请告诉我。JAVA
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
<标题> LOGCAT h1> div class="one_answers">
ImageButtons现在都是null
,因为正确的内容视图还没有设置。
onFinish()
方法中设置OnClickListener
s。所以你的代码应该是:
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;
}
}
标题>