我在主 Activity 中有 home 和 exit 按钮,当我单击 Exit 按钮时,应用程序关闭。但是当我单击主页按钮时,它会得到 RunTimeException。
这是MainActivity的onCreate
方法代码
public void onCreate(Bundle savedInstanceState) {
home = (Button) findViewById(R.id.home);
exit_app = (Button) findViewById(R.id.exit_app);
home.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent1 = new Intent(MainActivity.this,
FirstActivity.class);
startActivity(intent1);
}
});
exit_app.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
这是 FirstActivity 的onCreate
方法:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.firstactivitylayout);
home = (Button) findViewById(R.id.home);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
exit_app = (Button) findViewById(R.id.exit_app);
home.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(FirstActivity.this,MainActivity.class);
startActivity(intent);
}
});
exit_app.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}