我的代码
public class start extends AppCompatActivity {
@Override
protected void onCreate(Bundle ici) {
super.onCreate(ici);
setContentView(R.layout.activity_start);
final Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
Intent myIntent = new Intent(start.this, QuestionActivity.class);
start.this.startActivity(myIntent);
}
});
不确定是否找到按钮但是没有错误
final Button button5 = (Button) findViewById(R.id.button5);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
Intent myIntent = new Intent(start.this, QuestionActivity.class);
start.this.startActivity(myIntent);
}
});
}
如果单击按钮,则没有任何反应 如果我只是写
Intent myIntent = new Intent(start.this, QuestionActivity.class);
start.this.startActivity(myIntent);
有效。
我是编程的新手,所以请善待:) 请帮忙
答案 0 :(得分:1)
首先定义button
并设置监听器,然后定义button5
并再次将监听器设置为button
。所以button5
什么都不做。真正的代码如下:
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
Intent myIntent = new Intent(start.this, QuestionActivity.class);
start.this.startActivity(myIntent);
}
});
final Button button5 = (Button) findViewById(R.id.button5);
button5.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
Intent myIntent = new Intent(start.this, QuestionActivity.class);
start.this.startActivity(myIntent);
}
});
答案 1 :(得分:1)
检查按钮名称....
final Button button5 = (Button) findViewById(R.id.button5);
// use button5 instead of button below one
button5.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
Intent myIntent = new Intent(start.this, QuestionActivity.class);
start.this.startActivity(myIntent);
}
});