【问题标题】:android java calling button from XML without getting NULLandroid java从XML调用按钮而不获取NULL
【发布时间】:2015-04-18 08:52:02
【问题描述】:

我希望调用几个按钮,但在尝试 findbyviewid 时似乎得到了 NULL。当我激活此活动时,它会崩溃。

//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 个按钮,并且希望能够为它们设置文本,并为这组按钮运行一个侦听器(问题的答案)。单击其中一个按钮时,它应该通过拉动状态(真/假)来确定它是否是正确的答案,如果它不正确则将其突出显示为红色。然后它将正确答案突出显示为绿色。

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

【问题讨论】:

  • 在Activity的onCreate方法中调用所有findViewById方法
  • 即使这样做了,我也无法在该类/活动的其他方法中使用按钮变量。

标签: java android button null


【解决方案1】:

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

【讨论】:

  • 知道了。说得通。正如你所知道的那样,我的 Java 效率并不高。但是我将如何声明一个按钮,以便我可以在整个活动中使用它,并在 onCreate() 方法中初始化按钮? Button buttonOne; onCreate() 之前和buttonOne = (Button) findViewById(R.id.answerOne); onCreate() 内部:???
【解决方案2】:

正如我在评论中所说的那样,您应该在OnCreate 方法中对其进行初始化,因为您在这里为活动设置了视图布局。在你这样做之前,所有findViewById都返回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);
    [...]
}

【讨论】:

  • 我太专注于按钮,忽略了 TextView。感谢您花时间澄清这么简单的事情。
猜你喜欢
  • 2015-02-01
  • 2017-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-14
相关资源
最近更新 更多