如果我点击按钮,没有任何反应

时间:2016-10-08 10:41:05

标签: java android

我的代码

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);

有效。

我是编程的新手,所以请善待:) 请帮忙

2 个答案:

答案 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);
        }
    });