-1

Hello out there you helping guys let me please tell you that I am very new to Android and Java. I have writen a OnClickHandler and it compiles and starts but if I press one button it stops.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);

    int[] buttons = {R.id.button1, R.id.button2, R.id.button3, R.id.button4, R.id.button5,  R.id.button6, R.id.button7, R.id.button8, R.id.button9};

    for (int i = 0; i < buttons.length; i++) {
        Button buttonNum = (Button) this.findViewById(buttons[i]);
        buttonNum.setOnClickListener(new onCklickHandler());
    }
}

class onCklickHandler implements View.OnClickListener {
    public void onClick(View v) {
        if ("On" == button.getText()){
            button.setText("Off"); 
        }
        else {
            button.setText("On");
        }
        v.playSoundEffect(SoundEffectConstants.CLICK);
    }
}

Can anyone help?

  • Post your stacktrace. But probably NPE since I don't see `button` declared anywhere – codeMagic Oct 23 '14 at 15:01
  • 2
    We need the logcat contents please. – BenjaminPaul Oct 23 '14 at 15:01
  • Also, it should be `button.getText().toString()` and `.equals()` is the correct way to compare `Strings` in Java. `if ("On".equals(button.getText().toString()) {`. See [comparing Strings in Java](http://stackoverflow.com/questions/19432553/if-edittext-gettext-tostring-dont-work/19432569#19432569) – codeMagic Oct 23 '14 at 15:03
  • 1
    In my opinion, whatever `button` is, it's null – ToYonos Oct 23 '14 at 15:07
  • what is this button here – Ansar Oct 23 '14 at 15:08
  • If this is the code you're using, it's going to break because you don't close the `onCreate` function's curly brace... Ultimately, we need more code, both of the class and the stack trace (as others before me have said). – Nathan White Oct 23 '14 at 15:12
  • possible duplicate of [Unfortunately MyApp has stopped. How can I solve this?](http://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) – nhaarman Oct 23 '14 at 16:07
  • class onClickHandler implements View.OnClickListener { public void onClick(View v) { Button button = (Button) v.findViewById(v.getId()); if ("On" == button.getText()){ button.setText("Off"); } else { button.setText("On"); } v.playSoundEffect(SoundEffectConstants.CLICK); } } – Oliver Guhse Oct 23 '14 at 16:59

2 Answers2

0

My failure was that the call Button.setText did not had a ID so I had to find first the correct Id

        Button button = (Button) v.findViewById(v.getId());

Thats it ...

Thanks a lot for that many postings and your answers. The Handler is now :

 class onClickHandler implements View.OnClickListener {
    public void onClick(View v) {
        Button button = (Button) v.findViewById(v.getId());
        if ("On" == button.getText()){
            button.setText("Off");
        }
        else {
            button.setText("On");
        }
        v.playSoundEffect(SoundEffectConstants.CLICK);
    }
}
-1

I think that button variable is undefined

Patrizio
  • 145
  • 4
  • This would not cause the app to close, the conditional statement would simply keep the label of "On". The issue seems to be that button is undefined. – BenjaminPaul Oct 23 '14 at 15:55