通过Intent调用类

时间:2017-06-06 00:51:31

标签: java android

主要方法按钮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);
            }
        });

在此代码运行后,问题类onCreate很好,但Answers类不运行。我使用相同的按钮来调用两者的意图。我最终只是试图从问题类中的类调用方法,这导致了一个错误。是否存在意图问题,或者Answers类中的代码是否存在问题?

问题

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

数目:

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

1 个答案:

答案 0 :(得分:1)

  

意图存在问题

是。一次只有一个活动活动,所以这没有意义。

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

我最终只是试图从问题类中的类中调用导致错误的方法

这段代码?

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

正确......可能是错误,因为findViewById没有上下文,因为你在一个Activity上调用了new(提示:不这样做)。

您可能已经Answers extends Activity,因此可能会调用 findViewById,这可能无法按您预期的方式工作。

另外,您没有实现onCreate,因此无论如何都无法查找。

  

Answers类无法运行

Answers类应该做什么并不是很清楚,但我认为你不需要单独的Activity来保存4个按钮并计算结果。

您可以在一个活动中将问题和可能的答案放在一个布局中。在使用其他类进行更复杂的操作之前,先从中开始。

不幸的是,这里没有一般解决方案。它归结为学习如何更好地组织代码。

可能的建议阅读:Activities are not UI elements