2017-07-14 43 views
1

我正在做一個測驗應用程序,我用一個EditText,當我不輸入任何東西和SubmitButton我得到這個錯誤:無法爲Android的執行方法:的onClick

FATAL EXCEPTION: main 
Process: com.example.andriod.quiz, PID: 12960 
java.lang.IllegalStateException: Could not execute method for android:onClick 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293) at android.view.View.performClick(View.java:4756) 
at android.view.View$PerformClick.run(View.java:19761) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5264) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:900) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:695) 
Caused by: java.lang.reflect.InvocationTargetException 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
at android.view.View.performClick(View.java:4756) 
at android.view.View$PerformClick.run(View.java:19761) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5264) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:900) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:695) 
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x0 
at android.content.res.Resources.getText(Resources.java:284) 
at android.widget.TextView.setText(TextView.java:4176) 
at com.example.andriod.quiz.MainActivity.submitAnswer(MainActivity.java:65) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
at android.view.View.performClick(View.java:4756) 
at android.view.View$PerformClick.run(View.java:19761) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5264) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:900) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:695) 

這裏是我的Java代碼:

package com.example.andriod.quiz; 
import android.content.Context; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.view.inputmethod.InputMethodManager; 
import android.widget.CheckBox; 
import android.widget.EditText; 
import android.widget.LinearLayout; 
import android.widget.RadioButton; 
import android.widget.TextView; 
import android.widget.Toast; 

/** 
* This app displays Millionaire Quiz 
*/ 
public class MainActivity extends AppCompatActivity { 

private String name; 
private int correctAnswers; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
} 

/** 
* This method is called when the Ok button is clicked. it's displays personalized greetings. 
* 
* @param view 
*/ 
public void greetings(View view) { 
    EditText nameField = (EditText) findViewById(R.id.customer_name); 
    name = nameField.getText().toString(); 
    TextView greetings = (TextView) findViewById(R.id.greetings); 
    greetings.setText("Hello " + name + ". Scroll down if you are ready."); 

    InputMethodManager inputManager = (InputMethodManager) 
      getSystemService(Context.INPUT_METHOD_SERVICE); 

    inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 
      InputMethodManager.HIDE_NOT_ALWAYS); 

    LinearLayout questionsLayout = (LinearLayout) findViewById(R.id.questions_layout); 
    questionsLayout.setVisibility(View.VISIBLE); 
} 


/** 
* Checks correct answers, and displays a toast with congratulations and amount of correctly answered questions. 
* 
* @param view 
*/ 
public void submitAnswer(View view) { 
    correctAnswers = 0; 
    RadioButton firstQuestion = (RadioButton) findViewById(R.id.first_correct); 
    if (firstQuestion.isChecked()) { 
     correctAnswers++; 
    } 

    EditText secondQuestion = (EditText) findViewById(R.id.second_question); 

      int answer = Integer.parseInt(secondQuestion.getText().toString()); 


    if (answer == 27) { 
     correctAnswers++; 
    } 

    CheckBox checkBoxA = (CheckBox) findViewById(R.id.answer_a); 
    CheckBox checkBoxB = (CheckBox) findViewById(R.id.answer_b); 
    CheckBox checkBoxC = (CheckBox) findViewById(R.id.answer_c); 
    CheckBox checkBoxD = (CheckBox) findViewById(R.id.answer_d); 
    if (!checkBoxA.isChecked() && checkBoxB.isChecked() && checkBoxC.isChecked() && !checkBoxD.isChecked()) { 
     correctAnswers++; 
    } 

    RadioButton fourthQuestion = (RadioButton) findViewById(R.id.fourth_correct); 
    if (fourthQuestion.isChecked()) { 
     correctAnswers++; 
    } 

    RadioButton fifthQuestion = (RadioButton) findViewById(R.id.fifth_correct); 
    if (fifthQuestion.isChecked()) { 
     correctAnswers++; 
    } 

    String correctlyAnswered; 
    switch (correctAnswers) { 
     case 5: 
      correctlyAnswered = "Congratulation " + name + " you answer correctly to every question! You won 1 million! "; 
      break; 
     case 4: 
      correctlyAnswered = "Congratulation " + name + " you answer correctly to 4 questions! You won 750 thousands! You should try one more time!"; 
      break; 
     case 3: 
      correctlyAnswered = "Congratulation " + name + " you answer correctly to 3 questions! You won 500 thousands! You should try one more time!"; 
      break; 
     case 2: 
      correctlyAnswered = "Congratulation " + name + " you answer correctly to 3 questions! You won 250 thousands! You should try one more time!"; 
      break; 
     case 1: 
      correctlyAnswered = "Congratulation " + name + " you answer correctly to 1 question! You won 100 thousands! You should try one more time!"; 
      break; 
     default: 
      correctlyAnswered = "You didn't answer correctly to any question :(\nYou should try one more time!"; 
    } 
    Toast toast = Toast.makeText(this, correctlyAnswered, Toast.LENGTH_LONG); 
    toast.show(); 
} 
} 

這裏是xml文件:

 <?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/second_activity" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="#6158AC" 
android:orientation="vertical" 
tools:context="com.example.andriod.quiz.MainActivity"> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:padding="16dp"> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:paddingBottom="16dp" 
     android:text="Welcome in Millionaire Game! \nAre you ready to play for a million?" 
     android:textSize="24sp" /> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 

     <EditText 
      android:id="@+id/customer_name" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:hint="Name" 
      android:inputType="textCapWords" /> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:onClick="greetings" 
      android:text="Ok" 
      android:textAllCaps="true" /> 
    </LinearLayout> 

    <TextView 
     android:id="@+id/greetings" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:paddingBottom="16dp" 
     android:paddingTop="16dp" 
     android:textSize="24sp" /> 

    <LinearLayout 
     android:id="@+id/questions_layout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
     android:visibility="visible"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="First question: \nHow many parts has Harry Potter series?" /> 

     <RadioGroup 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"> 

      <RadioButton 
       android:id="@+id/first_correct" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Seven" /><!--correct--> 
      <RadioButton 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Eight" /> 

      <RadioButton 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Six" /> 

      <RadioButton 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Three" /> 
     </RadioGroup> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Second question: \nOwen thinks of a number, adds 13, and then divides the result by 5. The answer is 8. Find the number Owen thinks of." /> 

     <EditText 
      android:id="@+id/second_question" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:inputType="number" 
      android:hint="Write here your answer." /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Third question: \nWhat is a value of absolute zero?" /> 

     <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:orientation="vertical"> 

      <CheckBox 
       android:id="@+id/answer_a" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="-189°C" /> 

      <CheckBox 
       android:id="@+id/answer_b" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="-271,15°C" /><!--correct--> 
      <CheckBox 
       android:id="@+id/answer_c" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="0K" /><!--correct--> 
      <CheckBox 
       android:id="@+id/answer_d" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="100K" /> 
     </LinearLayout> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Fourth question: \nWhat is the main component in glass?" /> 

     <RadioGroup 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"> 

      <RadioButton 
       android:id="@+id/fourth_correct" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Sand" /> <!--correct--> 
      <RadioButton 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Iron" /> 

      <RadioButton 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Coal" /> 

      <RadioButton 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Water" /> 
     </RadioGroup> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Fifth question: \nWhich is the largest species of the tiger?" /> 

     <RadioGroup 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"> 

      <RadioButton 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Chinese tiger" /> 

      <RadioButton 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Bengali tiger" /> 

      <RadioButton 
       android:id="@+id/fifth_correct" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Siberian tiger" /><!--correct--> 
      <RadioButton 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Indo-Chinese tiger" /> 
     </RadioGroup> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:onClick="submitAnswer" 
      android:text="Submit answers" 
      android:textAllCaps="true" /> 

    </LinearLayout> 
</LinearLayout> 

有誰知道爲什麼這個錯誤發生? 我爲每一個錯誤,我做了對不起,這是我的第一個問題在這裏;)

+0

嘗試刪除'greetings(View view)'方法內的所有代碼,以檢查此代碼是否由於onClick方法或此代碼中的某些內容而失敗。我遇到了同樣的問題。在我的情況下,堆棧跟蹤顯示不正確。 – Vyacheslav

+0

Mateusz,考慮接受我的答案,它應該可以幫助你:)謝謝 – Konrad

+0

這個異常不會發生在你提供的代碼中。它在'submitAnswer'內列出了一個對'setText'的調用,它不存在。這表明它可能運行一些過時的代碼而不是當前的版本。 – Kiskae

回答

0

它失敗在這裏:

int answer = Integer.parseInt(secondQuestion.getText().toString()); 

你必須在這裏有一個不爲空場,也應該是一個數字。

你可以做簡單的檢查:

int answer = 0; 
EditText secondQuestion = (EditText) findViewById(R.id.second_question); 

if(secondQuestion.getText().toString().equals("")) { 
    //Handle invalid input 
} else { 
    answer = Integer.parseInt(secondQuestion.getText().toString()); 
} 

只實現無效輸入一些驗證,這樣就可以很容易地進行處理。

+0

這是正確的答案。您將一個空字符串傳遞給Integer.parseInt()。它不能將空字符串轉換爲整數,並且會得到異常。 –

+0

我檢查了你的建議,很不幸沒有工作:/我得到了同樣的異常。 –

+1

@MateuszTurek奇怪的是,我剛剛將你的代碼複製到新創建的項目中,並且它正在工作。也許嘗試做同樣的事情?或者只是重建項目 - 如果你應用了我的建議 – Konrad

相關問題