简体   繁体   中英

Nothing happens if i click the button

My Code

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

Not sure if it finds the button but ther is no error

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


}

Nothing happens if I click the Button.
If I just write

           Intent myIntent = new Intent(start.this, QuestionActivity.class);
           start.this.startActivity(myIntent);

It works.

I am new to programming so please be kind :)
Please help

you first define button and set the listener but then you define button5 and set the listener to button again. so button5 wont do anything. the true code is like below:

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

check the button name ....

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM