在方法中实现按钮onClickListener

时间:2012-03-06 19:34:13

标签: java android button nullpointerexception onclicklistener

我在尝试实现onClickListener时遇到错误,这是错误: 03-06 19:21:20.432:E / AndroidRuntime(1197):引起:java.lang.NullPointerException 03-06 19:21:20.432:E / AndroidRuntime(1197):at com.coursework.braintrain.Game.Easy12(Game.java:186)

这是引起它的行:buttonhash.setOnClickListener(new OnClickListener(){

基本上我在onCreate()方法中创建了buttonhash,但是我想这样做,以便在按下按钮时,它会监听用户输入是否与等式的实际答案匹配。也许如果我只是把代码放在一起它会更有意义(不善于解释......)

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

}

}

我的XML很好,如果这是可能导致它的事情之一,但我认为它无论如何都与它无关:P

所以我的问题是在我设置我的buttonhash的onClickListener的行中获得nullpointerexception,这是另一种方法(Easy12())。任何有关可能导致此问题/如何解决问题的帮助都将非常感激。感谢

3 个答案:

答案 0 :(得分:1)

您在定义按钮之前调用了getGame(),因此它为null。 将按钮声明放在getGame上面,如下所示:

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

答案 1 :(得分:0)

如果它在该行上崩溃,则buttonhash为null。出于某种原因,在Oncreate中调用findViewById时未设置/找到它。确保id与xml中的id匹配。

确保在初始化后也调用getGame。

答案 2 :(得分:0)

将buttonhash.setOnClickListener(新的OnClickListener()更改为

buttonhash.setOnClickListener(new Veiw.OnClickListener()