2017-06-06 49 views
0

主要方法按鈕的onClick調用類:通過一個Intent

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類中的代碼有問題?

問題

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

你是治療'Answers'作爲一項活動,這只是錯在給定的情況下......有沒有意見,查找和** **從來沒有叫'在'Activity'類中新增了' –

+0

'您的'答案'活動是錯誤的,您應該像在'問題'中所做的那樣使用onCreate方法,並且不能像「Button choice1 =(Button)findViewById(R。 ID。answer_choice1);「在你的」答案「活動中 –

+0

我認爲你對於問題和答案都使用單一佈局,我是的你不需要'答案'活動,你可以使用問題活動本身來執行這兩個操作 –

回答

1

是否有與意圖

是一個問題。每次只有一項活動處於活動狀態,所以這沒有意義。

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

最後我只是試圖調用從類中導致錯誤

此代碼的問題類的方法?

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

權......這可能是錯誤的,因爲findViewById沒有上下文,因爲你上的活動叫new(提示:不這樣做)。

你可能做Answers extends Activity這樣你可以調用findViewById,這將不工作,你所期望的方式,大概。

再加上你沒有執行onCreate,所以沒有任何意見可以找到。

的解答類不運行

這不是真的清楚什麼答案類是應該做的,但我不認爲你需要一個單獨的活動舉辦4個按鈕和計算結果。

您可以在一個活動中將問題和可能的答案放在一個佈局中。從其他課程開始之前,你需要做更復雜的事情。

不幸的是,這裏沒有通用的解決方案。這涉及到如何更好地組織代碼。


可能推薦閱讀:Activities are not UI elements