0

How to handle with multi buttons?

I´m using this code to change SoundOff to soundOff ON click

I dont now how to fix the error

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    setContentView(R.layout.gameover);

    //setting the orientation to landscape
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);



    ImageView medaille = (ImageView) findViewById(R.id.medaille);
    //adding a click listener

    //Define Logout
    ImageButton LogOut = (ImageButton) findViewById(R.id.imageButton8);

    //Define SoundOn

    ImageButton SoundOn = (ImageButton) findViewById(R.id.imageButton9);

    //Define SoundOff

    ImageButton SoundOff = (ImageButton) findViewById(R.id.imageButton10);

    //getting the button
    ImageButton ButtonOk = (ImageButton) findViewById(R.id.imageButton11);

    LogOut.setOnClickListener(this);
    SoundOff.setOnClickListener(this);
    SoundOn.setOnClickListener(this);
    ButtonOk.setOnClickListener(this);

    // Version

    //define
    TextView txtView = (TextView) findViewById(R.id.tv_current_score);

    TextView txtsView = (TextView) findViewById(R.id.tv_current_score_value);
    txtsView.setText(String.valueOf(GameView.score));

    //Value
    TextView myText = (TextView) findViewById(R.id.tv_best_score_value);
    myText.setText(String.valueOf(GameView.Highscorer) );

    TextView txtViews = (TextView) findViewById(R.id.tv_best_score);


}



@Override


    public void onClick (View v){

    //Define Logout
    ImageButton LogOut = (ImageButton) findViewById(R.id.imageButton8);

    //Define SoundOn

    ImageButton SoundOn = (ImageButton) findViewById(R.id.imageButton9);

    //Define SoundOff

    ImageButton SoundOff = (ImageButton) findViewById(R.id.imageButton10);

    //getting the button
    ImageButton ButtonOk = (ImageButton) findViewById(R.id.imageButton11);
    switch (v.getId()) {

            case R.id.imageButton11:
                //starting game activity
                startActivity(new Intent(this, GameView.class));
                break;

            case R.id.imageButton4:
                //Logout
                startActivity(new Intent(this, GameLogout.class));
                // do your code
                break;
            case R.id.imageButton8:
                startActivity(new Intent(this, GameLogout.class));
                break;
            case R.id.imageButton9:
                play = false;
                SoundOn.setVisibility(View.GONE);//set visibility to false on create
                SoundOff.setVisibility(View.VISIBLE);
                break;
            case R.id.imageButton10:
                play = true;
                SoundOn.setVisibility(View.VISIBLE);//set visibility to false on create
                SoundOff.setVisibility(View.GONE);



        }

    }

I´m getting an error in NullPointerException in the "SoundOn.setVisibility(View.GONE);//set visibility to false on create"

  • It´s not duplicate, its an error in the onClick and i dont see what is. –  Aug 04 '16 at 14:10
  • It simply means your variable SoundOn is a null object – S.W. Aug 04 '16 at 14:11
  • i think you forgot to add `findViewById` to the SoundOn view. – M.Waqas Pervez Aug 04 '16 at 14:11
  • "SoundOn" view reference variable is not initialized properly. Initialize the "SoundOn" in your onCreate() method. – Karthi R Aug 04 '16 at 14:12
  • Can you give me the code? whell i have initialize with " ImageButton ButtonOk = (ImageButton) findViewById(R.id.imageButton11); ImageView medaille = (ImageView) findViewById(R.id.medaille); //adding a click listener //Define Logout ImageButton LogOut = (ImageButton) findViewById(R.id.imageButton8); " –  Aug 04 '16 at 14:13

2 Answers2

0

SoundOn is null; you need to make sure you initialize it. If you are using Activities a good place is to do it in onCreate after you inflate the view. With something like this:

SoundOn = findViewById(R.id.YourViewsId);
w.donahue
  • 10,575
  • 12
  • 54
  • 77
0

Find which view was updated is only half the work.
You need to inflate (get runtime object of the view) and modify (change the visibility).
Add this before you perform any action off changing the button's view (the top of your switch statments):

Button SoundOn = (Button) findViewById(R.id.soundOffBtnId) //Use ImageButton instead of Button depends on your code.
Button SoundOff = (Button) findViewById(R.id.soundOffBtnId)
Nir Duan
  • 5,310
  • 4
  • 20
  • 38