Ryan
Ryan

Reputation: 65

Calling a class through an Intent

Main method button onClick:

public void onClick(View v) {
                Intent numbers = new Intent(MainActivity.this, Questions.class);
                numbers.putExtra("randomNumber", rand);
                startActivity(numbers);
                Intent numbertoAnswer = new Intent(MainActivity.this, Answers.class);
                numbertoAnswer.putExtra("randomnumber", rand);
                startActivity(numbertoAnswer);
            }
        });

After this code runs the question class onCreate is fine, but the Answers class does not run. I used the same button to call intents to both. I ended up just trying to call the methods from the class in the question class which caused an error. Is there a problem with an intents or is there a problem with the code in the Answers class?

Question

    protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.game_view);
            int questionNumber = getIntent().getIntExtra("randomNumber", 1);
            question(questionNumber);

            TextView questiontv = (TextView) findViewById(R.id.Question);
            questiontv.setText(question(questionNumber));

            Answers answers = new Answers();
            answers.createQuestions();
            answers.buttonTextGetter();
}

Answers:

public class Answers extends Activity {

    Button choice1 = (Button) findViewById(R.id.answer_choice1);
    Button choice2 = (Button) findViewById(R.id.answer_choice2);
    Button choice3 = (Button) findViewById(R.id.answer_choice3);
    Button choice4 = (Button) findViewById(R.id.answer_choice4);
    int questionNumber = getIntent().getIntExtra("randomnumber", 1);
    Random rand = new Random();
    int randomCorrectButton = rand.nextInt(3) + 1;

    public void createQuestions() {

        AnswerPeople(questionNumber);
        if (answer == null) {
            AnswerNumbers(questionNumber);
        }
        switch (randomCorrectButton) {
            case 1:
                choice1.setText(answer);
                break;
            case 2:
                choice2.setText(answer);
                break;
            case 3:
                choice3.setText(answer);
                break;
            case 4:
                choice4.setText(answer);
                break;
        }
    }
        String AnswerChoice1 = randomAnswerButton1(randomCorrectButton, questionNumber, rand);
        String AnswerChoice2 = randomAnswerButton2(randomCorrectButton, questionNumber, rand);
        String AnswerChoice3 = randomAnswerButton3(randomCorrectButton, questionNumber, rand);
        String AnswerChoice4 = randomAnswerButton4(randomCorrectButton, questionNumber, rand);

        public void buttonTextGetter(){


        randomAnswerButton1(randomCorrectButton, questionNumber, rand);
        randomAnswerButton2(randomCorrectButton, questionNumber, rand);
        randomAnswerButton3(randomCorrectButton, questionNumber, rand);
        randomAnswerButton4(randomCorrectButton, questionNumber, rand);

        checkForRepeat(AnswerChoice1, AnswerChoice2, AnswerChoice3, AnswerChoice4, randomCorrectButton, questionNumber, rand);

        if (randomCorrectButton != 1) {
            choice1.setText(AnswerChoice1);
        } else if (randomCorrectButton != 2) {
            choice2.setText(AnswerChoice2);
        } else if (randomCorrectButton != 3) {
            choice3.setText(AnswerChoice3);
        } else if (randomCorrectButton != 4) {
            choice4.setText(AnswerChoice4);
        }
        }



    public void checkForRepeat(String AnswerChoice1,String AnswerChoice2, String AnswerChoice3, String AnswerChoice4,
                               int randomCorrectButton, int questionNumber, Random rand) {
        if(AnswerChoice1 == AnswerChoice2 || AnswerChoice1 == AnswerChoice3 || AnswerChoice1 ==
                AnswerChoice4 || AnswerChoice2 == AnswerChoice3 || AnswerChoice2 == AnswerChoice4 || AnswerChoice3 == AnswerChoice4) {
            randomAnswerButton1(randomCorrectButton, questionNumber, rand);
            randomAnswerButton2(randomCorrectButton, questionNumber, rand);
            randomAnswerButton3(randomCorrectButton, questionNumber, rand);
        }
    }

    public String randomAnswerButton1(int randomCorrectButton, int questionNumber, Random rand) {
        if (randomCorrectButton != 1) {
            int answernum = rand.nextInt(19) + 1;
            if (answernum != questionNumber)
                AnswerPeople(answernum);
            if (answer == null) {
                AnswerNumbers(answernum);
            }

        }
        String AnswerChoice1 = answer;
        return AnswerChoice1;
    }

    public String randomAnswerButton2(int randomCorrectButton, int questionNumber, Random rand) {
        if (randomCorrectButton != 2) {
            int answernum2 = rand.nextInt(19) + 1;
            if (answernum2 != questionNumber)
                AnswerPeople(answernum2);
            if (answer == null) {
                AnswerNumbers(answernum2);
            }
        }
        String AnswerChoice2 = answer;
        return AnswerChoice2;
    }

    public String randomAnswerButton3(int randomCorrectButton, int questionNumber, Random rand) {

        if (randomCorrectButton != 3) {
            int answernum3 = rand.nextInt(19) + 1;
            if (answernum3 != questionNumber)
                AnswerPeople(answernum3);
            if (answer == null) {
                AnswerNumbers(answernum3);
            }
        }
        String AnswerChoice3 = answer;
        return AnswerChoice3;

    }

    public String randomAnswerButton4(int randomCorrectButton, int questionNumber, Random rand) {
        if (randomCorrectButton != 4) {
            int answernum4 = rand.nextInt(19) + 1;
            if (answernum4 != questionNumber)
                AnswerPeople(answernum4);
            if (answer == null) {
                AnswerNumbers(answernum4);
            }

        }
        String AnswerChoice4 = answer;
        return AnswerChoice4;
    }

Upvotes: 0

Views: 68

Answers (1)

OneCricketeer
OneCricketeer

Reputation: 191711

Is there a problem with an intents

Yes. There is only ever one Activity active at a time, so this doesn't make sense.

startActivity(numbers);
...
startActivity(numbertoAnswer);

I ended up just trying to call the methods from the class in the question class which caused an error

This code?

Answers answers = new Answers();
answers.createQuestions();
answers.buttonTextGetter();

Right... It probably error because findViewById has no Context because you called new on an Activity (Hint: don't do that).

You probably made Answers extends Activity so that you could call findViewById, which won't work the way you expected, probably.

Plus there is no onCreate that you implemented, so there is no views to find anyway.

the Answers class does not run

It's not really clear what the Answers class is supposed to do, but I don't think you need a separate Activity to hold 4 buttons and calculate the result.

You can put the question and the possible answers in one layout, in one activity. Start with that before you make anything more complex with additional classes.

There is no general solution here, unfortunately. It comes down to learning how to better organize your code.


Possible suggested reading: Activities are not UI elements

Upvotes: 1

Related Questions