public class MainActivity extends Activity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_screen);
ImageButton x = (ImageButton) findViewById(R.id.imageButton1);
x.setOnClickListener(this);
}
public void onClick(View v) {
Intent intent = new Intent(
com.example.choiceisaseriousmatter.MainActivity.this,
Choice.class);
startActivity(intent);
}
@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;
}
}
此代码用于在单击Activity
时切换到另一个imageButton
。但每当我点击按钮时,程序就会崩溃。
答案 0 :(得分:1)
您是否已覆盖onClick方法?试试这个改变:
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Choice.class);
startActivity(intent);
}
答案 1 :(得分:0)