java.lang.RuntimeException: 无法启动 activity,无法访问 UI 元素
java.lang.RuntimeException: Unable to start activity, uable to acces the UI elements
当我试图访问我的 activity 中的 UI 元素时,它没有打开并抛出运行时异常并且应用程序崩溃。我再次重建了整个项目,但问题是一样的。
--XML代码
<?xml version="1.0" encoding="utf-8"?>
<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"
android:orientation="vertical"
tools:context=".BMIActivity">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar_bmi_activity"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:background="#FFFFFF" />
<LinearLayout
android:id="@+id/llUnitsView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/toolbar_bmi_activity"
android:orientation="vertical"
android:padding="10dp">
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/tilMetricUnitWeight"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.AppCompatEditText
android:id="@+id/etMetricUnitWeight"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="WEIGHT (in kg)"
android:inputType="numberDecimal"
android:textSize="16sp" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/tilMetricUnitHeight"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_marginTop="10dp"
android:layout_height="wrap_content">
<androidx.appcompat.widget.AppCompatEditText
android:id="@+id/etMetricUnitHeight"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="HEIGHT (in cm)"
android:inputType="numberDecimal"
android:textSize="16sp" />
</com.google.android.material.textfield.TextInputLayout>
<LinearLayout
android:id="@+id/llDiplayBMIResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:orientation="vertical"
android:visibility="visible"
tools:visibility="visible">
<TextView
android:id="@+id/tvYourBMI"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="YOUR BMI"
android:textAllCaps="true"
android:visibility="invisible"
android:textSize="16sp" />
<TextView
android:id="@+id/tvBMIValue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:gravity="center"
android:textSize="18sp"
android:textStyle="bold"
tools:text="15.00" />
<TextView
android:id="@+id/tvBMIType"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:gravity="center"
android:textSize="18sp"
tools:text="Normal" />
<TextView
android:id="@+id/tvBMIDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:gravity="center"
android:textSize="18sp"
tools:text="Normal" />
</LinearLayout>
<Button
android:id="@+id/btnCalculateUnits"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginTop="35dp"
android:background="@color/colorAccent"
android:text="CALCULATE"
android:textColor="#FFFFFF"
android:textSize="18sp" />
</LinearLayout>
</RelativeLayout>
--错误
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.a7minuteworkout, PID: 4810
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.a7minuteworkout/com.example.a7minuteworkout.BMIActivity}: android.view.InflateException: Binary XML file line #29 in com.example.a7minuteworkout:layout/activity_bmiactivity: Binary XML file line #29 in com.example.a7minuteworkout:layout/activity_bmiactivity: Error inflating class com.google.android.material.textfield.TextInputLayout
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3534)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3689)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:140)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:100)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2239)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:227)
at android.app.ActivityThread.main(ActivityThread.java:7822)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1026)
Caused by: android.view.InflateException: Binary XML file line #29 in com.example.a7minuteworkout:layout/activity_bmiactivity: Binary XML file line #29 in com.example.a7minuteworkout:layout/activity_bmiactivity: Error inflating class com.google.android.material.textfield.TextInputLayout
Caused by: android.view.InflateException: Binary XML file line #29 in com.example.a7minuteworkout:layout/activity_bmiactivity: Error inflating class com.google.android.material.textfield.TextInputLayout
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
at android.view.LayoutInflater.createView(LayoutInflater.java:855)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1012)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:963)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1142)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1103)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1145)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1103)
at android.view.LayoutInflater.inflate(LayoutInflater.java:682)
at android.view.LayoutInflater.inflate(LayoutInflater.java:534)
at android.view.LayoutInflater.inflate(LayoutInflater.java:481)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:699)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:195)
at com.example.a7minuteworkout.BMIActivity.onCreate(BMIActivity.kt:9)
at android.app.Activity.performCreate(Activity.java:7963)
at android.app.Activity.performCreate(Activity.java:7952)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1306)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3505)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3689)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:140)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:100)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2239)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:227)
at android.app.ActivityThread.main(ActivityThread.java:7822)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1026)
Caused by: java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.MaterialComponents (or a descendant).
at com.google.android.material.internal.ThemeEnforcement.checkTheme(ThemeEnforcement.java:243)
at com.google.android.material.internal.ThemeEnforcement.checkMaterialTheme(ThemeEnforcement.java:217)
at com.google.android.material.internal.ThemeEnforcement.checkCompatibleTheme(ThemeEnforcement.java:145)
at com.google.android.material.internal.ThemeEnforcement.obtainTintedStyledAttributes(ThemeEnforcement.java:115)
at com.google.android.material.textfield.TextInputLayout.<init>(TextInputLayout.java:469)
at com.google.android.material.textfield.TextInputLayout.<init>(TextInputLayout.java:428)
... 30 more
能找到issue指向的地方,找了很多答案还是找不到想要的结果。
请任何人帮我解决这个问题。
Logcat 有答案。它说
Caused by: java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.MaterialComponents (or a descendant).
改用Theme.MaterialComponents
主题Theme.AppCompat
更改为 Theme.MaterialComponents 将在应用程序 UI、颜色、尺寸等方面有很多变化。它们会很好。别担心。
如果您不想更改整个应用的主题,请尝试将其添加到相对布局。 (不推荐)
android:theme="@style/Theme.MaterialComponents.DayNight.DarkActionBar"
你的相对布局应该是:
<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"
android:orientation="vertical"
android:theme="@style/Theme.MaterialComponents.DayNight.DarkActionBar"
tools:context=".BMIActivity">
当我试图访问我的 activity 中的 UI 元素时,它没有打开并抛出运行时异常并且应用程序崩溃。我再次重建了整个项目,但问题是一样的。
--XML代码
<?xml version="1.0" encoding="utf-8"?>
<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"
android:orientation="vertical"
tools:context=".BMIActivity">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar_bmi_activity"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:background="#FFFFFF" />
<LinearLayout
android:id="@+id/llUnitsView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/toolbar_bmi_activity"
android:orientation="vertical"
android:padding="10dp">
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/tilMetricUnitWeight"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.AppCompatEditText
android:id="@+id/etMetricUnitWeight"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="WEIGHT (in kg)"
android:inputType="numberDecimal"
android:textSize="16sp" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/tilMetricUnitHeight"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_marginTop="10dp"
android:layout_height="wrap_content">
<androidx.appcompat.widget.AppCompatEditText
android:id="@+id/etMetricUnitHeight"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="HEIGHT (in cm)"
android:inputType="numberDecimal"
android:textSize="16sp" />
</com.google.android.material.textfield.TextInputLayout>
<LinearLayout
android:id="@+id/llDiplayBMIResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:orientation="vertical"
android:visibility="visible"
tools:visibility="visible">
<TextView
android:id="@+id/tvYourBMI"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="YOUR BMI"
android:textAllCaps="true"
android:visibility="invisible"
android:textSize="16sp" />
<TextView
android:id="@+id/tvBMIValue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:gravity="center"
android:textSize="18sp"
android:textStyle="bold"
tools:text="15.00" />
<TextView
android:id="@+id/tvBMIType"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:gravity="center"
android:textSize="18sp"
tools:text="Normal" />
<TextView
android:id="@+id/tvBMIDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:gravity="center"
android:textSize="18sp"
tools:text="Normal" />
</LinearLayout>
<Button
android:id="@+id/btnCalculateUnits"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginTop="35dp"
android:background="@color/colorAccent"
android:text="CALCULATE"
android:textColor="#FFFFFF"
android:textSize="18sp" />
</LinearLayout>
</RelativeLayout>
--错误
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.a7minuteworkout, PID: 4810
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.a7minuteworkout/com.example.a7minuteworkout.BMIActivity}: android.view.InflateException: Binary XML file line #29 in com.example.a7minuteworkout:layout/activity_bmiactivity: Binary XML file line #29 in com.example.a7minuteworkout:layout/activity_bmiactivity: Error inflating class com.google.android.material.textfield.TextInputLayout
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3534)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3689)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:140)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:100)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2239)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:227)
at android.app.ActivityThread.main(ActivityThread.java:7822)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1026)
Caused by: android.view.InflateException: Binary XML file line #29 in com.example.a7minuteworkout:layout/activity_bmiactivity: Binary XML file line #29 in com.example.a7minuteworkout:layout/activity_bmiactivity: Error inflating class com.google.android.material.textfield.TextInputLayout
Caused by: android.view.InflateException: Binary XML file line #29 in com.example.a7minuteworkout:layout/activity_bmiactivity: Error inflating class com.google.android.material.textfield.TextInputLayout
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
at android.view.LayoutInflater.createView(LayoutInflater.java:855)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1012)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:963)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1142)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1103)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1145)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1103)
at android.view.LayoutInflater.inflate(LayoutInflater.java:682)
at android.view.LayoutInflater.inflate(LayoutInflater.java:534)
at android.view.LayoutInflater.inflate(LayoutInflater.java:481)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:699)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:195)
at com.example.a7minuteworkout.BMIActivity.onCreate(BMIActivity.kt:9)
at android.app.Activity.performCreate(Activity.java:7963)
at android.app.Activity.performCreate(Activity.java:7952)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1306)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3505)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3689)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:140)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:100)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2239)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:227)
at android.app.ActivityThread.main(ActivityThread.java:7822)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1026)
Caused by: java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.MaterialComponents (or a descendant).
at com.google.android.material.internal.ThemeEnforcement.checkTheme(ThemeEnforcement.java:243)
at com.google.android.material.internal.ThemeEnforcement.checkMaterialTheme(ThemeEnforcement.java:217)
at com.google.android.material.internal.ThemeEnforcement.checkCompatibleTheme(ThemeEnforcement.java:145)
at com.google.android.material.internal.ThemeEnforcement.obtainTintedStyledAttributes(ThemeEnforcement.java:115)
at com.google.android.material.textfield.TextInputLayout.<init>(TextInputLayout.java:469)
at com.google.android.material.textfield.TextInputLayout.<init>(TextInputLayout.java:428)
... 30 more
能找到issue指向的地方,找了很多答案还是找不到想要的结果。 请任何人帮我解决这个问题。
Logcat 有答案。它说
Caused by: java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.MaterialComponents (or a descendant).
改用Theme.MaterialComponents
主题Theme.AppCompat
更改为 Theme.MaterialComponents 将在应用程序 UI、颜色、尺寸等方面有很多变化。它们会很好。别担心。
如果您不想更改整个应用的主题,请尝试将其添加到相对布局。 (不推荐)
android:theme="@style/Theme.MaterialComponents.DayNight.DarkActionBar"
你的相对布局应该是:
<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"
android:orientation="vertical"
android:theme="@style/Theme.MaterialComponents.DayNight.DarkActionBar"
tools:context=".BMIActivity">