【问题标题】:android.view.InflateException: Binary XML file line #55android.view.InflateException:二进制 XML 文件行 #55
【发布时间】:2021-08-12 10:56:37
【问题描述】:

单击一个按钮以在另一个布局中添加线性布局

点击监听器是:

question_next.setOnClickListener {
            if(validate()){
                question = question_question.text.toString()
                marks = question_marks.text.toString()
                answer = questions_answer.text.toString()

                val questionModel = QuestionModel(question, marks,option_list,null,answer)
                question_list.add(questionModel)

                CreateAndAppendQuestionListLayout()       //62
                total_mark += marks.toInt()
                total_mark_view.text = "Total marks: "+total_mark.toString()
                question_question.text!!.clear()
                question_marks.text!!.clear()
                questions_answer.text!!.clear()
                option_question.removeAllViews()
                option_list.clear()
            }
        }


fun CreateAndAppendQuestionListLayout(){
        val finalQuestion = findViewById<LinearLayout>(R.id.final_question)
        val layoutInfate = applicationContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        for(i in question_list.indices){
            val tempQuestion = layoutInfate.inflate(R.layout.question_,null)      //125
            val questionNumber = tempQuestion.findViewById<TextView>(R.id.question_layout_number)
            val questionStatement = tempQuestion.findViewById<TextView>(R.id.question_layout_question)
            val questionMark = tempQuestion.findViewById<TextView>(R.id.question_layout_mark)
            val optionView = tempQuestion.findViewById<LinearLayout>(R.id.question_layout_option)

            questionNumber.text = (i+1).toString()
            questionStatement.text = question_list[i].question.toString()
            questionMark.text = question_list[i].mark.toString()
            val optionList = question_list[i].option as MutableList
            for(j in optionList.indices){
                val tempOption = layoutInfate.inflate(R.layout.option_,null)
                val optionNumber = tempOption.findViewById<TextView>(R.id.option_no)
                val optionText = tempOption.findViewById<TextView>(R.id.option_text)

                optionNumber.text = (j+1).toString()
                optionText.text = optionList[j]

                optionView.addView(tempOption)
            }


            finalQuestion.addView(tempQuestion)
        }
    }

每当我单击按钮时,我的应用程序都会崩溃。 logcat的状态是:

2021-05-24 10:39:40.671 6013-6013/com.example.sikyann E/AndroidRuntime: 致命异常: main 进程:com.example.sikyann,PID:6013 android.view.InflateException:com.example.sikyann 中的二进制 XML 文件第 55 行:layout/question_:二进制 XML 文件第 55 行 com.example.sikyann:layout/question_: 膨胀类错误 引起:android.view.InflateException:com.example.sikyann 中的二进制 XML 文件第 55 行:layout/question_:膨胀类错误 引起:java.lang.reflect.InvocationTargetException 在 java.lang.reflect.Constructor.newInstance0(本机方法) 在 java.lang.reflect.Constructor.newInstance(Constructor.java:343) 在 android.view.LayoutInflater.createView(LayoutInflater.java:852) 在 android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1008) 在 android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:959) 在 android.view.LayoutInflater.rInflate(LayoutInflater.java:1138) 在 android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1099) 在 android.view.LayoutInflater.inflate(LayoutInflater.java:680) 在 android.view.LayoutInflater.inflate(LayoutInflater.java:532) 在 android.view.LayoutInflater.inflate(LayoutInflater.java:479) 在 com.example.sikyann.Activity.CreateQuestionPaperActivity.CreateAndAppendQuestionListLayout(CreateQuestionPaperActivity.kt:125) 在 com.example.sikyann.Activity.CreateQuestionPaperActivity$onCreate$2.onClick(CreateQuestionPaperActivity.kt:62) 在 android.view.View.performClick(View.java:7580) 在 android.view.View.performClickInternal(View.java:7531) 在 android.view.View.access$3900(View.java:842) 在 android.view.View$PerformClick.run(View.java:28871) 在 android.os.Handler.handleCallback(Handler.java:938) 在 android.os.Handler.dispatchMessage(Handler.java:99) 在 android.os.Looper.loop(Looper.java:255) 在 android.app.ActivityThread.main(ActivityThread.java:8214) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:632) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1049) 原因:java.lang.IllegalArgumentException:此组件上的样式要求您的应用主题为 Theme.AppCompat(或 后裔)。 在 com.google.android.material.internal.ThemeEnforcement.checkTheme(ThemeEnforcement.java:221) 在 com.google.android.material.internal.ThemeEnforcement.checkAppCompatTheme(ThemeEnforcement.java:196) 在 com.google.android.material.internal.ThemeEnforcement.checkCompatibleTheme(ThemeEnforcement.java:131) 在 com.google.android.material.internal.ThemeEnforcement.obtainTintedStyledAttributes(ThemeEnforcement.java:110) 在 com.google.android.material.textfield.TextInputLayout.(TextInputLayout.java:266) 在 com.google.android.material.textfield.TextInputLayout.(TextInputLayout.java:247) 在 java.lang.reflect.Constructor.newInstance0(本机方法) 在 java.lang.reflect.Constructor.newInstance(Constructor.java:343) 在 android.view.LayoutInflater.createView(LayoutInflater.java:852) 在 android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1008) 在 android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:959) 在 android.view.LayoutInflater.rInflate(LayoutInflater.java:1138) 在 android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1099) 在 android.view.LayoutInflater.inflate(LayoutInflater.java:680) 在 android.view.LayoutInflater.inflate(LayoutInflater.java:532) 在 android.view.LayoutInflater.inflate(LayoutInflater.java:479) 在 com.example.sikyann.Activity.CreateQuestionPaperActivity.CreateAndAppendQuestionListLayout(CreateQuestionPaperActivity.kt:125) 在 com.example.sikyann.Activity.CreateQuestionPaperActivity$onCreate$2.onClick(CreateQuestionPaperActivity.kt:62) 在 android.view.View.performClick(View.java:7580) 在 android.view.View.performClickInternal(View.java:7531) 在 android.view.View.access$3900(View.java:842) 在 android.view.View$PerformClick.run(View.java:28871) 在 android.os.Handler.handleCallback(Handler.java:938) 在 android.os.Handler.dispatchMessage(Handler.java:99) 在 android.os.Looper.loop(Looper.java:255) 在 android.app.ActivityThread.main(ActivityThread.java:8214) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:632) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1049)

【问题讨论】:

    标签: android


    【解决方案1】:

    原因:java.lang.IllegalArgumentException:此组件上的样式要求您的应用主题为 Theme.AppCompat(或后代)。

    您需要使用派生自 Theme.AppCompat 的主题。无论如何,您可能不应该为父视图传递 null 。您最终将其添加到 finalQuestion 中,只需将 finalQuestion 作为父级传入即可。这将使它使用来自 finalQuestion 的主题,并自动为您添加它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多