android java 从 XML 调用按钮但没有得到 NULL

android java calling button from XML without getting NULL

我想调用几个按钮,但在尝试 findbyviewid 时似乎得到了 NULL。当我激活这个 activity 时,它崩溃了。

//CREATE INSTANCE OF GLOBAL - QUESTIONS/ANSWERS
Global global = Global.getInstance();

//CURRENT QUESTION
static int QQ = 0;

//CORRECT ANSWER COUNT
static int correctAnswers = 0;

//CREATE VARIABLE FOR TEXTVIEW/QUESTION
TextView textQuestion = (TextView) findViewById(R.id.textQuestion);

//CREATE VARIABLES FOR BUTTONS/ANSWERS
Button buttonOne = (Button) findViewById(R.id.answerOne);
Button buttonTwo = (Button) findViewById(R.id.answerTwo);
Button buttonThree = (Button) findViewById(R.id.answerThree);
Button buttonFour = (Button) findViewById(R.id.answerFour);



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


    setButtons();



    buttonOne.setOnClickListener(this);
    buttonTwo.setOnClickListener(this);
    buttonThree.setOnClickListener(this);
    buttonFour.setOnClickListener(this);



}





public void setButtons()
{

    //SET QUESTION STRING TO TEXTVIEW
    textQuestion.setText(global.getQ(QQ));

    //SET ANSWER STRINGS TO BUTTONS' TEXT
    buttonOne.setText(global.getA(QQ, 0));
    buttonTwo.setText(global.getA(QQ, 1));
    buttonThree.setText(global.getA(QQ, 2));
    buttonFour.setText(global.getA(QQ, 3));

}





@Override
public void onClick(View v)
{

    switch(v.getId())
    {
        case R.id.answerOne:
            checkAnswer(0, buttonOne);
            break;

        case R.id.answerTwo:
            checkAnswer(1, buttonTwo);
            break;

        case R.id.answerThree:
            checkAnswer(2, buttonThree);
            break;

        case R.id.answerFour:
            checkAnswer(3, buttonFour);
            break;

        default:
            break;
    }


}




public void checkAnswer(int a, Button b){

    //IF AN INCORRECT ANSWER WAS CHOSEN, MAKE THE BACKGROUND RED
    if(!global.getS(QQ, a))
    {
        b.setBackgroundColor(Color.RED);
    }
    else
    {
        //INCREMENT THE CORRECT ANSWER COUNTER
        correctAnswers++;
    }

    //SET BACKGROUND OF CORRECT BUTTON TO GREEN
    if(global.getS(QQ, 0))
    {
        buttonOne.setBackgroundColor(Color.GREEN);
    }
    else if(global.getS(QQ, 1))
    {
        buttonTwo.setBackgroundColor(Color.GREEN);
    }
    else if(global.getS(QQ, 2))
    {
        buttonThree.setBackgroundColor(Color.GREEN);
    }
    else if(global.getS(QQ, 3))
    {
        buttonFour.setBackgroundColor(Color.GREEN);
    }
    else
    {
        //IF NO ANSWER IS CORRECT, SET ALL TO BLUE
        buttonOne.setBackgroundColor(Color.BLUE);
        buttonTwo.setBackgroundColor(Color.BLUE);
        buttonThree.setBackgroundColor(Color.BLUE);
        buttonFour.setBackgroundColor(Color.BLUE);

    }



    //MOVE TO NEXT QUESTION


}

我在 XML 文件中有 4 个按钮,我希望能够为它们设置文本,以及 运行 一组按钮的侦听器(问题的答案) .当其中一个按钮被点击时,它应该通过拉动状态 (true/false) 来确定它是否是正确的答案,如果不正确则将其突出显示为红色。然后它将正确答案突出显示为绿色。

至少,其中一些是理论上的,我正在尝试对其进行测试,但我无法在不崩溃的情况下启动 activity。

我不是 100% 确定,但我认为您不能在实例构造中执行 findViewById。你需要 onCreate() 里面的那些(在你调用 setContentView 之后)

就像我在评论中所说的那样,你应该在 OnCreate 方法中初始化它,因为你在这里设置了 activity 的视图布局。在你这样做之前,所有 findViewById returns null.

所以,这是您的代码:

Button buttonOne;
Button buttonTwo;
Button buttonThree;
Button buttonFour;
TextView textQuestion;

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

    buttonOne = (Button) findViewById(R.id.answerOne);
    buttonTwo = (Button) findViewById(R.id.answerTwo);
    buttonThree = (Button) findViewById(R.id.answerThree);
    buttonFour = (Button) findViewById(R.id.answerFour);
    textQuestion = (TextView) findViewById(R.id.textQuestion);
    [...]
}