错误:膨胀类 com.google.android.material.textfield.TextInputLayout

Posted

技术标签:

【中文标题】错误:膨胀类 com.google.android.material.textfield.TextInputLayout【英文标题】:Error: Inflating class com.google.android.material.textfield.TextInputLayout 【发布时间】:2021-07-19 22:33:12 【问题描述】:

我收到了错误,这是我的 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_
    android:layout_
    android:orientation="vertical"
    tools:context=".BMIActivity">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar_bmi_activity"
        android:layout_
        android:layout_
        android:background="#FFFFFF" />

    <RadioGroup
        android:id="@+id/rgUnits"
        android:layout_
        android:layout_
        android:layout_below="@id/toolbar_bmi_activity"
        android:layout_margin="10dp"
        android:background="@drawable/drawable_radio_group_tab_background"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/rbMetricUnits"
            android:layout_
            android:layout_
            android:layout_weight="0.50"
            android:background="@drawable/drawable_units_tab_selector"
            android:button="@null"
            android:checked="true"
            android:gravity="center"
            android:text="METRIC UNITS"
            android:textColor="@drawable/drawable_units_tab_text_color_selector"
            android:textSize="16sp"
            android:textStyle="bold" />

        <RadioButton
            android:id="@+id/rbUsUnits"
            android:layout_
            android:layout_
            android:layout_weight="0.50"
            android:background="@drawable/drawable_units_tab_selector"
            android:button="@null"
            android:checked="false"
            android:gravity="center"
            android:text="US UNITS"
            android:textColor="@drawable/drawable_units_tab_text_color_selector"
            android:textSize="16sp"
            android:textStyle="bold" />
    </RadioGroup>

    <LinearLayout
        android:id="@+id/llUnitsView"
        android:layout_
        android:layout_
        android:layout_below="@id/rgUnits"
        android:orientation="vertical"
        android:padding="10dp">

        <LinearLayout
            android:id="@+id/llMetricUnitsView"
            android:layout_
            android:layout_
            android:orientation="vertical">

            <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/tilMetricUnitWeight"
                style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
                android:layout_
                android:layout_>

                <androidx.appcompat.widget.AppCompatEditText
                    android:id="@+id/etMetricUnitWeight"
                    android:layout_
                    android:layout_
                    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_
                android:layout_
                android:layout_marginTop="10dp">

                <androidx.appcompat.widget.AppCompatEditText
                    android:id="@+id/etMetricUnitHeight"
                    android:layout_
                    android:layout_
                    android:hint="HEIGHT (in cm)"
                    android:inputType="numberDecimal"
                    android:textSize="16sp" />
            </com.google.android.material.textfield.TextInputLayout>
        </LinearLayout>

        <LinearLayout
            android:id="@+id/llUsUnitsView"
            android:layout_
            android:layout_
            android:orientation="vertical"
            android:visibility="gone"
            tools:visibility="visible">

            <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/tilUsUnitWeight"
                style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
                android:layout_
                android:layout_>

                <androidx.appcompat.widget.AppCompatEditText
                    android:id="@+id/etUsUnitWeight"
                    android:layout_
                    android:layout_
                    android:hint="WEIGHT (in lbs)"
                    android:inputType="numberDecimal"
                    android:textSize="16sp" />
            </com.google.android.material.textfield.TextInputLayout>

            <LinearLayout
                android:id="@+id/llUsUnitsHeight"
                android:layout_
                android:layout_
                android:layout_marginTop="10dp"
                android:baselineAligned="false"
                android:orientation="horizontal">

                <com.google.android.material.textfield.TextInputLayout
                    android:id="@+id/tilUsUnitHeightFeet"
                    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
                    android:layout_
                    android:layout_
                    android:layout_marginEnd="5dp"
                    android:layout_weight="1">

                    <androidx.appcompat.widget.AppCompatEditText
                        android:id="@+id/etUsUnitHeightFeet"
                        android:layout_
                        android:layout_
                        android:hint="Feet"
                        android:inputType="number"
                        android:textSize="16sp" />
                </com.google.android.material.textfield.TextInputLayout>

                <com.google.android.material.textfield.TextInputLayout
                    android:id="@+id/tilUsUnitHeightInch"
                    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
                    android:layout_
                    android:layout_
                    android:layout_marginStart="5dp"
                    android:layout_weight="1">

                    <androidx.appcompat.widget.AppCompatEditText
                        android:id="@+id/etUsUnitHeightInch"
                        android:layout_
                        android:layout_
                        android:hint="Inch"
                        android:inputType="number"
                        android:textSize="16sp" />
                </com.google.android.material.textfield.TextInputLayout>

            </LinearLayout>
        </LinearLayout>

        <LinearLayout
            android:id="@+id/llDiplayBMIResult"
            android:layout_
            android:layout_
            android:layout_marginTop="25dp"
            android:orientation="vertical"
            android:visibility="visible"
            tools:visibility="visible">

            <TextView
                android:id="@+id/tvYourBMI"
                android:layout_
                android:layout_
                android:gravity="center"
                android:text="YOUR BMI"
                android:textAllCaps="true"
                android:visibility="invisible"
                android:textSize="16sp" />

            <TextView
                android:id="@+id/tvBMIValue"
                android:layout_
                android:layout_
                android:layout_marginTop="5dp"
                android:gravity="center"
                android:textSize="18sp"
                android:textStyle="bold"
                tools:text="15.00" />

            <TextView
                android:id="@+id/tvBMIType"
                android:layout_
                android:layout_
                android:layout_marginTop="5dp"
                android:gravity="center"
                android:textSize="18sp"
                tools:text="Normal" />

            <TextView
                android:id="@+id/tvBMIDescription"
                android:layout_
                android:layout_
                android:layout_marginTop="5dp"
                android:gravity="center"
                android:textSize="18sp"
                tools:text="Normal" />
        </LinearLayout>

        <Button
            android:id="@+id/btnCalculateUnits"
            android:layout_
            android:layout_
            android:layout_gravity="bottom"
            android:layout_marginTop="35dp"
            android:background="@color/colorAccent"
            android:text="CALCULATE"
            android:textColor="#FFFFFF"
            android:textSize="18sp" />
    </LinearLayout>
</RelativeLayout>

这里是styles.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
</resources>

这是堆栈跟踪

2021-04-29 16:49:39.077 5682-5682/? I/nminutesworkou: Not late-enabling -Xcheck:jni (already on)
2021-04-29 16:49:39.090 5682-5682/? I/nminutesworkou: Unquickening 12 vdex files!
2021-04-29 16:49:39.100 5682-5682/? W/nminutesworkou: Unexpected CPU variant for X86 using defaults: x86
2021-04-29 16:49:39.434 5682-5682/com.example.sevenminutesworkout D/NetworkSecurityConfig: No Network Security Config specified, using platform default
2021-04-29 16:49:39.436 5682-5682/com.example.sevenminutesworkout D/NetworkSecurityConfig: No Network Security Config specified, using platform default
2021-04-29 16:49:39.466 5682-5710/com.example.sevenminutesworkout D/libEGL: loaded /vendor/lib/egl/libEGL_emulation.so
2021-04-29 16:49:39.472 5682-5710/com.example.sevenminutesworkout D/libEGL: loaded /vendor/lib/egl/libGLESv1_CM_emulation.so
2021-04-29 16:49:39.478 5682-5710/com.example.sevenminutesworkout D/libEGL: loaded /vendor/lib/egl/libGLESv2_emulation.so
2021-04-29 16:49:39.585 5682-5682/com.example.sevenminutesworkout W/nminutesworkou: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (greylist, reflection, allowed)
2021-04-29 16:49:39.588 5682-5682/com.example.sevenminutesworkout W/nminutesworkou: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (greylist, reflection, allowed)
2021-04-29 16:49:39.655 5682-5708/com.example.sevenminutesworkout D/HostConnection: HostConnection::get() New Host Connection established 0xf6a1f110, tid 5708
2021-04-29 16:49:39.659 5682-5708/com.example.sevenminutesworkout D/HostConnection: HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_native_sync_v2 ANDROID_EMU_native_sync_v3 ANDROID_EMU_native_sync_v4 ANDROID_EMU_dma_v1 ANDROID_EMU_direct_mem ANDROID_EMU_host_composition_v1 ANDROID_EMU_host_composition_v2 ANDROID_EMU_vulkan ANDROID_EMU_deferred_vulkan_commands ANDROID_EMU_vulkan_null_optional_strings ANDROID_EMU_vulkan_create_resources_with_requirements ANDROID_EMU_YUV_Cache ANDROID_EMU_async_unmap_buffer ANDROID_EMU_vulkan_ignored_handles ANDROID_EMU_vulkan_free_memory_sync ANDROID_EMU_vulkan_shader_float16_int8 ANDROID_EMU_vulkan_async_queue_submit GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_host_side_tracing ANDROID_EMU_async_frame_commands ANDROID_EMU_gles_max_version_2 
2021-04-29 16:49:39.661 5682-5708/com.example.sevenminutesworkout W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
2021-04-29 16:49:39.670 5682-5708/com.example.sevenminutesworkout D/EGL_emulation: eglCreateContext: 0xf6a1fb20: maj 2 min 0 rcv 2
2021-04-29 16:49:39.674 5682-5708/com.example.sevenminutesworkout D/EGL_emulation: eglMakeCurrent: 0xf6a1fb20: ver 2 0 (tinfo 0xf6d70170) (first time)
2021-04-29 16:49:39.698 5682-5708/com.example.sevenminutesworkout I/Gralloc4: mapper 4.x is not supported
2021-04-29 16:49:39.700 5682-5708/com.example.sevenminutesworkout D/HostConnection: createUnique: call
2021-04-29 16:49:39.700 5682-5708/com.example.sevenminutesworkout D/HostConnection: HostConnection::get() New Host Connection established 0xf6a1fb90, tid 5708
2021-04-29 16:49:39.722 5682-5708/com.example.sevenminutesworkout D/goldfish-address-space: allocate: Ask for block of size 0x100
2021-04-29 16:49:39.723 5682-5708/com.example.sevenminutesworkout D/goldfish-address-space: allocate: ioctl allocate returned offset 0x3fbcae000 size 0x2000
2021-04-29 16:49:39.728 5682-5708/com.example.sevenminutesworkout D/HostConnection: HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_native_sync_v2 ANDROID_EMU_native_sync_v3 ANDROID_EMU_native_sync_v4 ANDROID_EMU_dma_v1 ANDROID_EMU_direct_mem ANDROID_EMU_host_composition_v1 ANDROID_EMU_host_composition_v2 ANDROID_EMU_vulkan ANDROID_EMU_deferred_vulkan_commands ANDROID_EMU_vulkan_null_optional_strings ANDROID_EMU_vulkan_create_resources_with_requirements ANDROID_EMU_YUV_Cache ANDROID_EMU_async_unmap_buffer ANDROID_EMU_vulkan_ignored_handles ANDROID_EMU_vulkan_free_memory_sync ANDROID_EMU_vulkan_shader_float16_int8 ANDROID_EMU_vulkan_async_queue_submit GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_host_side_tracing ANDROID_EMU_async_frame_commands ANDROID_EMU_gles_max_version_2 
2021-04-29 16:49:44.812 5682-5682/com.example.sevenminutesworkout D/AndroidRuntime: Shutting down VM
2021-04-29 16:49:44.822 5682-5682/com.example.sevenminutesworkout E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.sevenminutesworkout, PID: 5682
    java.lang.RuntimeException: Unable to start activity ComponentInfocom.example.sevenminutesworkout/com.example.sevenminutesworkout.BMIActivity: android.view.InflateException: Binary XML file line #67 in com.example.sevenminutesworkout:layout/activity_bmi: Binary XML file line #67 in com.example.sevenminutesworkout:layout/activity_bmi: Error inflating class com.google.android.material.textfield.TextInputLayout
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
     Caused by: android.view.InflateException: Binary XML file line #67 in com.example.sevenminutesworkout:layout/activity_bmi: Binary XML file line #67 in com.example.sevenminutesworkout:layout/activity_bmi: Error inflating class com.google.android.material.textfield.TextInputLayout
     Caused by: android.view.InflateException: Binary XML file line #67 in com.example.sevenminutesworkout:layout/activity_bmi: 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:852)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1004)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:959)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:1121)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:1124)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:1124)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:680)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:532)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:479)
        at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:696)
        at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:170)
        at com.example.sevenminutesworkout.BMIActivity.onCreate(BMIActivity.kt:24)
        at android.app.Activity.performCreate(Activity.java:8000)
        at android.app.Activity.performCreate(Activity.java:7984)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
2021-04-29 16:49:44.824 5682-5682/com.example.sevenminutesworkout E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
     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:463)
        at com.google.android.material.textfield.TextInputLayout.<init>(TextInputLayout.java:422)
            ... 32 more

我尝试将 AppCompat 更改为 MaterialComponents,但它不起作用。但是当我指向 LinearLayout 标记(com.google.android.material.textfield.TextInputLayout 的父级)时,它会用红线显示错误,当我删除 LinearLayout 标记时它会消失。当我使用过多的 LinearLayout 相互叠加时会不会有什么问题?

【问题讨论】:

发布您的堆栈跟踪 @GabrieleMariotti 我做到了,请帮助我,谢谢 xD 此组件上的样式要求您的应用主题为 Theme.MaterialComponents(或后代)。您必须将应用主题更改为Theme.MaterialComponents.* 它根本不起作用:( 您是否更改了应用主题? 【参考方案1】:

您是否在应用级别的 build.gradle 文件中添加了材料依赖项?

如果没有,你需要在你的 gradle 文件中添加它。

api 'com.google.android.material:material:<VERSION>'

【讨论】:

我已经添加了``` implementation 'com.google.android.material:material:1.3.0' ```【参考方案2】:

确保在您的应用级别 build.gradle 文件中添加材质依赖项 并改变

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

【讨论】:

以上是关于错误:膨胀类 com.google.android.material.textfield.TextInputLayout的主要内容,如果未能解决你的问题,请参考以下文章

在 Eclipse 中膨胀类 com.google.android.gms.ads.Adview 错误

膨胀类 com.google.android.maps.MapView 时出错

膨胀类 com.google.android.material.slider.Slider 时出错

膨胀类 com.google.android.exoplayer2.ui.SimpleExoPlayerView 时出错

膨胀类 com.google.android.youtube.player.YouTubePlayerView 时出错

膨胀类 com.google.android.material.textfield.TextInputLayout 时出错