سؤال

I'm getting an error when trying to implement an onClickListener, this is the error: 03-06 19:21:20.432: E/AndroidRuntime(1197): Caused by: java.lang.NullPointerException 03-06 19:21:20.432: E/AndroidRuntime(1197):at com.coursework.braintrain.Game.Easy12(Game.java:186)

and this is the line that is causing it: buttonhash.setOnClickListener(new OnClickListener(){

Basically I made buttonhash in the onCreate() method, but I want to make it so that when the button is pressed, it listens to whether the users input matches the actual answer of the equation. Maybe if I just put the code up it will make more sense (not good at explaining...)

public class Game extends Activity {
private static final String TAG = "Brain Training";

public static final String KEY_DIFFICULTY = "com.coursework.braintrain.difficulty";
public static final int DIFFICULTY_EASY = 0;
public static final int DIFFICULTY_MEDIUM = 1;
public static final int DIFFICULTY_HARD = 2;
public static final int DIFFICULTY_GURU = 3;


private int brain;

private EditText edittext;
private Button button1;
private Button button2;
private Button button3;
private Button button4;
private Button button5;
private Button button6;
private Button button7;
private Button button8;
private Button button9;
private Button button0;

private TextView answerLabel;

private Button buttonhash;



@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    Log.d(TAG, "onCreate");
    setContentView(R.layout.gamelayout);

    int diff = getIntent().getIntExtra(KEY_DIFFICULTY, DIFFICULTY_EASY);
    getGame(diff);


    edittext = (EditText) findViewById(R.id.USERentry);

    answerLabel = (TextView) findViewById(R.id.rightwrong_label);

    button1 = (Button) findViewById(R.id.keypad_1);
    button1.setOnClickListener(new OnClickListener(){
        public void onClick(View v) {
            // Perform action on clicks
            //String buttonText = (String) button.getText();
            edittext.setText(edittext.getText() + "1");
            //edittext.setText() = edittext.getText() + "1";
        }
    });

    button2 = (Button) findViewById(R.id.keypad_2);
    button2.setOnClickListener(new OnClickListener(){
        public void onClick(View v) {

            edittext.setText(edittext.getText() + "2");
        }
    });

    button3 = (Button) findViewById(R.id.keypad_3);
    button3.setOnClickListener(new OnClickListener(){
        public void onClick(View v) {

            edittext.setText(edittext.getText() + "3");
        }
    });

    button4 = (Button) findViewById(R.id.keypad_4);
    button4.setOnClickListener(new OnClickListener(){
        public void onClick(View v) {

            edittext.setText(edittext.getText() + "4");
        }
    });

    button5 = (Button) findViewById(R.id.keypad_5);
    button5.setOnClickListener(new OnClickListener(){
        public void onClick(View v) {

            edittext.setText(edittext.getText() + "5");
        }
    });

    button6 = (Button) findViewById(R.id.keypad_6);
    button6.setOnClickListener(new OnClickListener(){
        public void onClick(View v) {

            edittext.setText(edittext.getText() + "6");
        }
    });

    button7 = (Button) findViewById(R.id.keypad_7);
    button7.setOnClickListener(new OnClickListener(){
        public void onClick(View v) {

            edittext.setText(edittext.getText() + "7");
        }
    });

    button8 = (Button) findViewById(R.id.keypad_8);
    button8.setOnClickListener(new OnClickListener(){
        public void onClick(View v) {

            edittext.setText(edittext.getText() + "8");
        }
    });

    button9 = (Button) findViewById(R.id.keypad_9);
    button9.setOnClickListener(new OnClickListener(){
        public void onClick(View v) {

            edittext.setText(edittext.getText() + "9");
        }
    });

    button0 = (Button) findViewById(R.id.keypad_0);
    button0.setOnClickListener(new OnClickListener(){
        public void onClick(View v) {

            edittext.setText(edittext.getText() + "0");
        }
    });

    buttonhash = (Button) findViewById(R.id.keypad_hash);


    //final Button buttonDel = (Button) findViewById(R.id.keypad_del);
    //buttonDel.setOnClickListener(new OnClickListener(){
        //public void onClick(View v){
            //edittext.setText(edittext.getText() - 1);

        //}
    //});
}

private void getGame(int diff){

    //TODO: Continue last game
    switch(diff){
    case DIFFICULTY_HARD:

        break;
    case DIFFICULTY_MEDIUM:

        break;
    case DIFFICULTY_EASY:
        Easy12();
        break;
    }
    //might require to return an array or something

}

public void Easy12(){
    Random rand = new Random();
    int a = (int) rand.nextInt(100)+1;
    int b = (int) rand.nextInt(100)+1;
    final TextView tv = (TextView) findViewById(R.id.question_label);
    String aString = Integer.toString(a);
    String bString = Integer.toString(b);
    String display = aString + " + " + bString + " =";
    tv.setText(display);
    final int c = a + b;
    buttonhash.setOnClickListener(new OnClickListener(){ //HERE IS THE ERROR!!

        public void onClick(View v) {

            if(edittext.getText().toString().equals(String.valueOf(c))){

                answerLabel.setText(R.string.answer_correct);
                answerLabel.setTextColor(R.color.correct_color);
                //answerLabel.setBackgroundColor(R.color.background_answer);
            }
            else
            {

                answerLabel.setText(R.string.answer_wrong);
                answerLabel.setTextColor(R.color.wrong_color);
                //answerLabel.setBackgroundColor(R.color.background_answer);
            }
        }
    });

}

}

My XML is fine if that is one of the things that might be causing it but I don't think it has anything to do with it anyway :P

So my problem is getting a nullpointerexception at the line where I set my buttonhash's onClickListener, which is in another method (Easy12()). Any help on what might be causing this/how to fix it would be much appreciated. Thanks

هل كانت مفيدة؟

المحلول

You're calling getGame() before defining your button, so it's null. Put the button declaration above getGame like this:

buttonhash = (Button) findViewById(R.id.keypad_hash);
getGame(diff)

نصائح أخرى

If it does crash on that line, then buttonhash is null. For some reason, it is not being set/found when you call findViewById in Oncreate. Make sure the ids match with the one in the xml.

Make sure you also call getGame after you initilize it.

change buttonhash.setOnClickListener(new OnClickListener() to

buttonhash.setOnClickListener(new Veiw.OnClickListener()

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top