0

I'm looking to call a few buttons but seem to be getting a NULL when trying to findbyviewid. When I activate this activity, it crashes.

//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


}

I have 4 buttons in the XML file and want to be able to set the text to them, as well as run a listener for the set of buttons (answers to a question). When one of the buttons is clicked, it should determine if it's the correct answer by pulling the status (true/false) and highlighting it red if it's incorrect. It then highlights the correct answer green.

At least, some of this is in theory and I'm trying to test it out, but I can't start the activity without crashing.

2
  • call all findViewById methods inside onCreate method of Activity Commented Feb 17, 2015 at 15:43
  • Even after doing so, I'm not able to use the button variables in other methods of that class/activity. Commented Feb 18, 2015 at 0:24

2 Answers 2

3

I'm not 100% sure but I think you can't do the findViewById at the instance constructions. You need to those inside onCreate() (after you called setContentView)

Sign up to request clarification or add additional context in comments.

1 Comment

Got it. Makes sense. I'm not that Java efficient as you can tell. But how would I declare a button so that I can use it throughout the activity, and initialize the button in the onCreate() method? Button buttonOne; before onCreate() and buttonOne = (Button) findViewById(R.id.answerOne); on the inside on onCreate():???
1

Just how i said in comment, you should initialize it in OnCreate method, cause you set view layout for activity here. And before you do it, all findViewById returns null.

So, here your code:

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);
    [...]
}

1 Comment

I was so focused on the buttons, I overlooked the TextView. Thanks for taking the time to clarify something so simple.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.