Android 添加后 TextInputLayout 应用程序崩溃 'style'

Android TextInputLayout Crashed app when I had added 'style'

android 的 material 个组件的 TextInputLayout 有问题。当我将 style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense" 添加到我的 TextInputLayout 时,应用程序崩溃了。所有代码 .xml 就像片段。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".CreatorFragment"
    android:background="@color/cardview_dark_background">

    <LinearLayout
        android:orientation="vertical"
        android:padding="20dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="match_parent"
            android:layout_height="164dp"
            android:foregroundGravity="center_vertical|center|center_horizontal"
            android:src="@drawable/ic_photo"
            />

        <com.google.android.material.textfield.TextInputLayout

            android:id="@+id/outlinedTextField"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            tools:layout_editor_absoluteX="1dp"
            tools:layout_editor_absoluteY="244dp"
            android:hint="Title"
            app:counterEnabled="true"
            app:counterMaxLength="18"
            app:counterOverflowTextColor="?attr/colorPrimary"
            android:textColorHint = "?attr/colorPrimary"
            app:hintTextColor = "?attr/colorPrimary"
            app:prefixTextColor = "?attr/colorPrimary"
            app:boxStrokeColor = "?attr/colorPrimary"
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
            >


            <com.google.android.material.textfield.TextInputEditText

                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                 />
        </com.google.android.material.textfield.TextInputLayout>


    </LinearLayout>

</FrameLayout>

我有清单:

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:requestLegacyExternalStorage="true"
        android:theme="@style/Theme.MaterialComponents.Light.DarkActionBar">

来自 logcat 的错误代码是:

2021-10-30 14:42:48.810 6850-6850/com.example.diary E/libc: Access denied finding property "ro.vendor.df.effect.conflict"
      0.2% 1421/msm_irqbalance: 0% user + 0.2% kernel
      0.2% 2678/com.android.phone: 0% user + 0.1% kernel / faults: 2 minor
      0.1% 692/zygote64: 0% user + 0.1% kernel / faults: 194 minor
      0.1% 3419/irq/81-90b6300.: 0% user + 0.1% kernel
      0% 10914/com.miui.securitycenter.remote: 0% user + 0% kernel / faults: 749 minor
      0.1% 1/init: 0% user + 0% kernel
      0.1% 27/ksoftirqd/2: 0% user + 0.1% kernel
      0% 793/android.hardware.health@2.0-service: 0% user + 0% kernel
      0.1% 22074/logcat: 0.1% user + 0% kernel
      0.1% 25230/kworker/2:0: 0% user + 0.1% kernel
      0% 8/ksoftirqd/0: 0% user + 0% kernel
      0% 12/rcuop/0: 0% user + 0% kernel
      0% 538/ueventd: 0% user + 0% kernel
      0% 813/vendor.qti.hardware.perf@2.0-service: 0% user + 0% kernel
      0% 867/mi_thermald: 0% user + 0% kernel
      0% 1220/statsd: 0% user + 0% kernel
      0% 2179/hvdcp_opti: 0% user + 0% kernel
      0% 3369/kworker/1:0: 0% user + 0% kernel
      0% 5029/transport: 0% user + 0% kernel
      0% 21135/kworker/u17:0: 0% user + 0% kernel
      0% 27171/com.miui.notification:remote: 0% user + 0% kernel
      0% 30321/com.google.android.youtube: 0% user + 0% kernel / faults: 6 minor
      0% 9/rcu_preempt: 0% user + 0% kernel
      0% 10/rcu_sched: 0% user + 0% kernel
      0% 13/rcuos/0: 0% user + 0% kernel
      0% 19/ksoftirqd/1: 0% user + 0% kernel
      0% 22/rcuop/1: 0% user + 0% kernel
      0% 30/rcuop/2: 0% user + 0% kernel
      0% 38/rcuop/3: 0% user + 0% kernel
      0% 560/servicemanager: 0% user + 0% kernel
      0% 691/netd: 0% user + 0% kernel / faults: 18 minor
      0% 759/qrtr_rx: 0% user + 0% kernel
      0% 803/android.hardware.wifi@1.0-service: 0% user + 0% kernel
      0% 1200/cameraserver: 0% user + 0% kernel
      0% 1440/qcrild: 0% user + 0% kernel / faults: 6 minor
      0% 2382/cds_ol_rx_threa: 0% user + 0% kernel
      0% 3252/kworker/3:1: 0% user + 0% kernel
      0% 3264/kworker/2:2: 0% user + 0% kernel
      0% 3331/charge_logger: 0% user + 0% kernel
      0% 16507/kworker/u17:1: 0% user + 0% kernel
      0% 17866/com.mi.android.globallauncher: 0% user + 0% kernel / faults: 3 minor
      0% 21372/kworker/0:0: 0% user + 0% kernel
      0% 30216/kworker/2:1: 0% user + 0% kernel
     +0% 5515/com.android.se: 0% user + 0% kernel
    3.3% TOTAL: 1.5% user + 1.3% kernel + 0% iowait + 0.3% irq + 0.1% softirq
    CPU usage from 1438307732ms to 1438307732ms ago (1970-01-01 01:00:00.000 to 1970-01-01 01:00:00.000) with 0% awake:
    0% TOTAL: 0% user + 0% kernel
2021-10-30 14:42:48.714 6850-6850/? E/m.example.diar: Unknown bits set in runtime_flags: 0x8000
2021-10-30 14:42:48.714 6850-6850/? E/m.example.diar: Unknown bits set in runtime_flags: 0x8000
2021-10-30 14:42:48.714 6850-6850/? E/m.example.diar: Unknown bits set in runtime_flags: 0x8000
2021-10-30 14:42:52.914 6850-6850/com.example.diary E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.diary, PID: 6850
    android.view.InflateException: Binary XML file line #40 in com.example.diary:layout/fragment_creator: Binary XML file line #40 in com.example.diary:layout/fragment_creator: Error inflating class com.google.android.material.textfield.TextInputLayout
    Caused by: android.view.InflateException: Binary XML file line #40 in com.example.diary:layout/fragment_creator: 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:858)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1010)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:965)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:1127)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1088)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:1130)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1088)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:686)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:538)
        at com.example.diary.CreatorFragment.onCreateView(CreatorFragment.kt:37)
        at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2963)
        at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:518)
        at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:282)
        at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189)
        at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2106)
        at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:2002)
        at androidx.fragment.app.FragmentManager.run(FragmentManager.java:524)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:224)
        at android.app.ActivityThread.main(ActivityThread.java:7565)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
     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)
        at java.lang.reflect.Constructor.newInstance0(Native Method) 
        at java.lang.reflect.Constructor.newInstance(Constructor.java:343) 
        at android.view.LayoutInflater.createView(LayoutInflater.java:858) 
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1010) 
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:965) 
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:1127) 
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1088) 
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:1130) 
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1088) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:686) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:538) 
        at com.example.diary.CreatorFragment.onCreateView(CreatorFragment.kt:37) 
        at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2963) 
        at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:518) 
        at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:282) 
        at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189) 
        at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2106) 
        at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:2002) 
        at androidx.fragment.app.FragmentManager.run(FragmentManager.java:524) 
        at android.os.Handler.handleCallback(Handler.java:883) 
        at android.os.Handler.dispatchMessage(Handler.java:100) 
        at android.os.Looper.loop(Looper.java:224) 
        at android.app.ActivityThread.main(ActivityThread.java:7565) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950) 

第 40 行在 TextInputLayout 中 'style' 之后。 我该如何解决?

将您的应用主题更改为 Theme.MaterialComponents 可能就没问题了

Stacktrace 异常原因从外部开始并逐渐进入 - 您首先看到的是导致崩溃的异常,但如果您往下读,您会看到 导致的原因 那个异常,然后是什么导致了 那个 等等,直到找到问题的根源。

如果你仔细阅读每个部分(从 Caused by 开始),你最终会到达此部分(最后一节,即问题开始的地方):

Caused by: java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.MaterialComponents (or a descendant).

这些信息可能就足够了! ;)