【问题标题】:Error inflating class RadioButton膨胀类 RadioButton 时出错
【发布时间】:2016-08-18 14:47:50
【问题描述】:

我正在使用 php mysql json 解析器创建测验应用程序,在运行程序中它显示“Caused by: android.view.InflateException: Binary XML file line #44: Error inflating class RadioButton”创建 xml 文件的错误。 我在QuizActivity.java 崩溃日志中使用这些代码会在创建内容视图和布局充气器时抛出错误

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

【问题讨论】:

  • 请添加崩溃日志
  • 不要使用@+id,除非你想给一个新的id。对于所有其他对视图的引用,只做 @id 。但我不认为这会导致这个错误....
  • 如果您之前进行了更改,请清理您的项目....
  • 可能是您的radio_bg 分辨率很高,请注意...其余代码都很好。
  • 你的 xml 代码中的这篇文章是确切的代码吗?

标签: java android xml radio-button


【解决方案1】:

我认为您错过了 &lt;RadioGroup&gt; 元素中的方向属性。试试吧,

android:orientation = "vertical"

在您的 RadioGroup> 元素中,然后尝试清理并重建您的项目。

【讨论】:

  • 我尝试使用这个 "android:orientation = "vertical" " 但没有任何改变
  • 我已经运行了您的代码,并且运行良好。问题可能出在您使用按钮属性指定的选择器上。确保您的选择器是正确的。否则你的代码没有问题。
【解决方案2】:

您好,请看这个答案:https://stackoverflow.com/a/46646047/6632278

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

【讨论】:

    【解决方案3】:

    我在设置自定义单选图标时遇到了同样的问题:

    android:button="@drawable/radio_bg"
    

    因为我错误地将radio_bg.xml 粘贴在drawable-v24 中,反之亦然,并且错误仅出现在旧版本中。所以将相同的radio_bg.xml 粘贴到共同的drawable 文件夹中解决了问题。

    【讨论】:

      猜你喜欢
      • 2020-01-05
      • 1970-01-01
      • 2019-11-29
      • 2017-02-28
      • 2021-07-10
      • 2020-08-01
      • 2016-02-28
      • 2013-05-17
      • 2016-09-22
      相关资源
      最近更新 更多