Error inflating class RadioButton

10 浏览
0 Comments

Error inflating class RadioButton

我正在使用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 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:




    
    
    
    
    

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

0
0 Comments

问题原因:如果你在v24/drawable文件夹中创建了radio_bg文件,那么你需要将其复制到drawable文件夹中以支持Android 7之前的设备。

解决方法:将radio_bg文件从v24/drawable文件夹复制到drawable文件夹中。

0
0 Comments

出现(Error inflating class RadioButton)这个问题的原因可能是在元素中缺少orientation属性。解决方法是在元素中添加android:orientation="vertical",然后进行项目的清理和重建。

代码示例:


    

另外,也有可能是你在button属性中指定的selector有问题。请确保你的selector正确无误。如果selector没有问题,则代码本身没有问题。

0
0 Comments

我在设置自定义单选按钮图标时遇到了相同的问题,出现在以下代码行中:

android:button="/radio_bg"

由于我错误地将radio_bg.xml 或反之亦然粘贴到了drawable-v24 文件夹中,而该错误仅在旧版本中出现。因此,将相同的radio_bg.xml 粘贴到常规的drawable 文件夹中解决了该问题。

0