2016-04-25 144 views
2

我創建測驗應用程序使用PHP的MySQL的JSON解析器,在該程序運行它顯示「Caused by: android.view.InflateException: Binary XML file line #44: Error inflating class RadioButton」創建xml文件上的錯誤。 我正在使用QuizActivity.java崩潰日誌這些代碼將拋出錯誤上創建內容爭奪和佈局吹氣錯誤膨脹類RadioButton

public class QuizActivity extends AppCompatActivity { 
private TextView quizQuestion; 
private RadioGroup radioGroup; 
private RadioButton optionOne; 
private RadioButton optionTwo; 
private RadioButton optionThree; 
private RadioButton optionFour; 
private int currentQuizQuestion; 
private int quizCount; 
private QuizWrapper firstQuestion; 
private List<QuizWrapper> parsedObject; 
@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_quiz); 
    // setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); quizQuestion = (TextView)findViewById(R.id.quiz_question); 
    radioGroup = (RadioGroup)findViewById(R.id.radioGroup); 
    optionOne = (RadioButton)findViewById(R.id.radio0); 
    optionTwo = (RadioButton)findViewById(R.id.radio1); 
    optionThree = (RadioButton)findViewById(R.id.radio2); 
    optionFour = (RadioButton)findViewById(R.id.radio3); 
    Button previousButton = (Button)findViewById(R.id.previousquiz); 
    Button nextButton = (Button)findViewById(R.id.nextquiz); 
    AsyncJsonObject asyncObject = new AsyncJsonObject(); 
    asyncObject.execute(""); 
    nextButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) 
     { 
      int radioSelected = radioGroup.getCheckedRadioButtonId(); 
      int userSelection = getSelectedAnswer(radioSelected); 
      int correctAnswerForQuestion = firstQuestion.getCorrectAnswer(); 
      if(userSelection == correctAnswerForQuestion){ 
       // correct answer 
       Toast.makeText(QuizActivity.this, "You got the answer correct", Toast.LENGTH_LONG).show(); 
       currentQuizQuestion++; 
       if(currentQuizQuestion >= quizCount){ 
        Toast.makeText(QuizActivity.this, "End of the Quiz Questions", Toast.LENGTH_LONG).show(); 
        return; 
       } 
       else{ 
        firstQuestion = parsedObject.get(currentQuizQuestion); 
        quizQuestion.setText(firstQuestion.getQuestion()); 
        String[] possibleAnswers = firstQuestion.getAnswers().split(","); 
        uncheckedRadioButton(); 
        optionOne.setText(possibleAnswers[0]); 
        optionTwo.setText(possibleAnswers[1]); 
        optionThree.setText(possibleAnswers[2]); 
        optionFour.setText(possibleAnswers[3]); 
       } 
      } 
      else{ 
       // failed question 
       Toast.makeText(QuizActivity.this, "You chose the wrong answer", Toast.LENGTH_LONG).show(); 
       return; 
      } 
     } 
    }); 
    previousButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      currentQuizQuestion--; 
      if(currentQuizQuestion < 0){ 
       return; 
      } 
      uncheckedRadioButton(); 
      firstQuestion = parsedObject.get(currentQuizQuestion); 
      quizQuestion.setText(firstQuestion.getQuestion()); 
      String[] possibleAnswers = firstQuestion.getAnswers().split(","); 
      optionOne.setText(possibleAnswers[0]); 
      optionTwo.setText(possibleAnswers[1]); 
      optionThree.setText(possibleAnswers[2]); 
      optionFour.setText(possibleAnswers[3]); 
     } 
    }); 
} 

XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context=".QuizActivity"> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/question" 
    android:id="@+id/quiz_question" 
    android:layout_alignParentTop="true" 
    android:layout_marginLeft="10dp" 
    android:layout_marginRight="10dp" 
    android:layout_marginEnd="10dp" 
    android:layout_marginStart="10dp" 
    android:layout_marginTop="20dp" 
    android:textSize="20sp" 
    android:textColor="#000000" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" /> 

<RadioGroup 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/quiz_question" 
    android:layout_alignLeft="@+id/quiz_question" 
    android:layout_alignStart="@+id/quiz_question" 
    android:layout_marginTop="40dp" 
    android:id="@+id/radioGroup"> 

    <RadioButton 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:id="@+id/radio0" 
     android:textSize="15sp" 
     android:textColor="#000000" 
     android:text="@string/app_name" 
     android:layout_marginBottom="10dp" 
     android:paddingLeft="20dp" 
     android:button="@drawable/radio_bg" 
     android:checked="false" /> 

    <RadioButton 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:id="@+id/radio1" 
     android:textSize="15sp" 
     android:textColor="@color/black" 
     android:text="@string/app_name" 
     android:layout_marginBottom="10dp" 
     android:paddingLeft="20dp" 
     android:button="@drawable/radio_bg" 
     android:checked="false" /> 

    <RadioButton 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:id="@+id/radio2" 
     android:textSize="15sp" 
     android:textColor="@color/black" 
     android:text="@string/app_name" 
     android:layout_marginBottom="10dp" 
     android:paddingLeft="20dp" 
     android:button="@drawable/radio_bg" 
     android:checked="false" /> 

    <RadioButton 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:id="@+id/radio3" 
     android:textSize="15sp" 
     android:textColor="@color/black" 
     android:text="@string/app_name" 
     android:paddingLeft="20dp" 
     android:button="@drawable/radio_bg" 
     android:checked="false" /> 

    </RadioGroup> 

<Button 
    android:layout_height="wrap_content" 
    android:layout_width="160dp" 
    android:gravity="center" 
    android:id="@+id/nextquiz" 
    android:textColor="@color/white" 
    android:text="@string/next_questions" 
    android:background="@drawable/quizbutton" 
    android:layout_marginRight="10dp" 
    android:padding="5dp" 
    android:layout_alignParentRight="true" 
    android:layout_alignBaseline="@+id/previousquiz"/> 

<Button 
    android:layout_height="wrap_content" 
    android:layout_width="160dp" 
    android:gravity="center" 
    android:id="@+id/previousquiz" 
    android:textColor="@color/white" 
    android:text="@string/previous_questions" 
    android:background="@drawable/quizbutton" 
    android:layout_below="@+id/radioGroup" 
    android:layout_alignLeft="@+id/radioGroup" 
    android:padding="5dp" 
    android:layout_marginTop="20dp" 
    android:layout_alignStart="@+id/radioGroup" /> 

Caused by: android.view.InflateException: Binary XML file line #45: Error inflating class RadioButton 
+0

請加崩潰日誌 – USKMobility

+0

不要使用'@ + id',除非你想給一個新的'id'。對於所有其他對視圖的引用,只能執行'@ id'。但我不認爲這會導致這個錯誤.... – Opiatefuchs

+0

,如果你已經做出了更改,清理你的項目.... – Opiatefuchs

回答

0

我想你已經錯過了方向屬性<RadioGroup>元素。嘗試,

android:orientation = "vertical" 

您< RadioGroup>元素內,然後嘗試清理和重建項目。

+1

我試圖使用這個「android:orientation =」vertical「」,但沒有任何變化 –

+0

我已經運行你的代碼,它的工作完美。可能問題在於你用button屬性指定了選擇器。確保你的選擇器是正確的。否則,在你的代碼中沒有問題。 –

相關問題