2

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
Gnana Prakash
  • 101
  • 1
  • 13

3 Answers3

0

I think you have missed orientation attribute in <RadioGroup> element. Try,

android:orientation = "vertical"

inside your <RadioGroup> element and then try to clean and rebuild your project.

Megha Maniar
  • 444
  • 5
  • 22
  • 1
    I tried to use of this "android:orientation = "vertical" " but nothing change – Gnana Prakash Apr 26 '16 at 07:03
  • I have run your code and it is working perfectly. May be the problem is with selector you have specified with button attribute. Make sure your selector is correct. Otherwise there is no issue in your code. – Megha Maniar Apr 26 '16 at 08:11
0

Hello please see this answer : https://stackoverflow.com/a/46646047/6632278

If you have created the file radio_bg in v24/drawable you must copy in drawable too for support to android devices before version 7

Tony Barajas
  • 124
  • 2
  • 12
0

I had the same problem while setting custom radio icons in line:

android:button="@drawable/radio_bg"

Because I mistakenly pasted radio_bg.xml or vice versa In drawable-v24 and the error was only in older versions. so pasting same radio_bg.xml in common drawable folder fixed the problem.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Jawad Zeb
  • 523
  • 4
  • 18