Thomas
Thomas

Reputation: 293

Button not working, How to resolve

I have a form with 6 buttons on it, and I am currently trying to get the 3rd to work. The first two buttons work correctly but when I click on the third button, nothing at all happens, and I get no errors. Can anyone help?

I have checked the form names, class names and button names but I can't get it to work. I have included the code for the buttons.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setting fullscreen
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN );
    setContentView(R.layout.main_menu);

    initialize(); //calling this method
}

public void initialize(){

    //Initializing variables

    Film = (Button) findViewById(R.id.bFilm);
    Music = (Button) findViewById(R.id.bMusic);
    Poets = (Button) findViewById(R.id.bPoet);
    Inventors = (Button) findViewById(R.id.bInventors);
    Science = (Button) findViewById(R.id.bScience);
    Sports = (Button) findViewById(R.id.bSport);

    //initialzing onclicklisteners
    Film.setOnClickListener(this);
    Music.setOnClickListener(this);
    Sports.setOnClickListener(this);
}

@Override
public void onClick(View view) {
    //case statment for onclicklisteners
    try{
    switch(view.getId()){
    case R.id.bFilm:
        Intent film = new Intent(this, Film.class); //open film class
        startActivity(film);
        break;
    case R.id.bMusic:
        Intent music = new Intent(this, Music.class); //open music class
        startActivity(music);
        break;
    case R.id.bPoet:
        Intent poet = new Intent(this, Poets.class); //open poet class
        startActivity(poet);
        break;
    case R.id.bInventors:
        Intent invent = new Intent(this, Inventors.class); //open inventors class
        startActivity(invent);
        break;
    case R.id.bScience:
        Intent science = new Intent(this, Science.class); // open science class
        startActivity(science);
        break;
    case R.id.bSport:
        Intent sport = new Intent(this, Sports.class);  //open sports class
        startActivity(sport);
        break;
}

Upvotes: 0

Views: 65

Answers (2)

JafarKhQ
JafarKhQ

Reputation: 8734

If Sports is your 3rd button
Make sure you dont have any element in your xml with the id bSport expect the bSport button itself.

Remove the try{} catch(){}. and see if your app crash, and make sure you have added Sports activity to the Manifest

Upvotes: 2

Wajahat Karim
Wajahat Karim

Reputation: 61

You have only set click listeners for 3 buttons (Film, Music, and Sports), you have to set all the listeners in your initlialize.

enter code here

public void initialize(){

    //Initializing variables

    Film = (Button) findViewById(R.id.bFilm);
    Music = (Button) findViewById(R.id.bMusic);
    Poets = (Button) findViewById(R.id.bPoet);
    Inventors = (Button) findViewById(R.id.bInventors);
    Science = (Button) findViewById(R.id.bScience);
    Sports = (Button) findViewById(R.id.bSport);

    //initialzing onclicklisteners
    Film.setOnClickListener(this);
    Music.setOnClickListener(this);
    Sports.setOnClickListener(this);
    Poets.setOnClickListener(this);
    Inventors.setOnClickListener(this);
    Science.setOnClickListener(this);
}

Upvotes: 2

Related Questions