Error inflating class RadioButton

时间:2016-04-25 09:31:50

标签: java android xml radio-button

I am creating quiz app using php mysql json parsor, In that ran the program it shows "Caused by: android.view.InflateException: Binary XML file line #44: Error inflating class RadioButton" the error on create xml file. I am using these codes in QuizActivity.java crash log will throw the error on create content vie and layout inflater

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

3 个答案:

答案 0 :(得分:0)

我认为您错过了<RadioGroup>元素中的方向属性。尝试,

android:orientation = "vertical"

在&lt; RadioGroup>元素中,然后尝试清理并重建项目。

答案 1 :(得分:0)

您好,请参见以下答案:https://stackoverflow.com/a/46646047/6632278

如果您在v24 / drawable中创建了文件radio_bg,则还必须在drawable中复制该文件,以支持版本7之前的Android设备

答案 2 :(得分:0)

在行中设置自定义单选图标时遇到相同的问题:

android:button="@drawable/radio_bg"

因为我在radio_bg.xml中错误地粘贴了drawable-v24,反之亦然,并且该错误仅发生在较旧的版本中。因此将相同的radio_bg.xml粘贴到公用drawable文件夹中即可解决此问题。