0

我有两个项目,一个在 NavigationView 中使用颜色状态列表选择器在不同的交互状态下更改 Navigation Drawer 中项目的背景颜色。

<com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:itemBackground="@color/nav_bgstate_list"  <------ 
        app:itemIconTint="@color/nav_state_list"  <------
        app:itemTextColor="@color/nav_state_list"  <------
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer"  />

        These three lines causes error but when I remove them, it works fine. 


    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <!-- Item checked -->
        <item android:color="@color/brand" android:state_checked="true" />
        <!-- Item pressed -->
        <item android:color="@color/black" android:state_pressed="true" />
        <!-- Item default -->
        <item android:color="@color/brand70" />
    </selector>



我将相同的代码复制到另一个项目中,它开始抛出一堆错误。日志如下

    2021-06-09 07:49:13.564 12895-12895/ca.company.project.betaversion E/AndroidRuntime: FATAL EXCEPTION: main
        Process: ca.company.project.betaversion, PID: 12895
        java.lang.RuntimeException: Unable to start activity ComponentInfo{ca.company.project.betaversion/ca.company.project.betaversion.MainActivity}: android.view.InflateException: Binary XML file line #26: Binary XML file line #26: Error inflating class com.google.android.material.navigation.NavigationView
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2781)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2859)
            at android.app.ActivityThread.-wrap11(Unknown Source:0)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1592)
            at android.os.Handler.dispatchMessage(Handler.java:106)
            at android.os.Looper.loop(Looper.java:164)
            at android.app.ActivityThread.main(ActivityThread.java:6518)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
         Caused by: android.view.InflateException: Binary XML file line #26: Binary XML file line #26: Error inflating class com.google.android.material.navigation.NavigationView
         Caused by: android.view.InflateException: Binary XML file line #26: Error inflating class com.google.android.material.navigation.NavigationView
         Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Constructor.newInstance0(Native Method)
            at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
            at android.view.LayoutInflater.createView(LayoutInflater.java:647)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
            at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
            at ca.company.project.betaversion.databinding.ActivityMainBinding.inflate(ActivityMainBinding.java:57)
            at ca.company.project.betaversion.databinding.ActivityMainBinding.inflate(ActivityMainBinding.java:51)
            at ca.company.project.betaversion.MainActivity.onCreate(MainActivity.kt:111)
            at android.app.Activity.performCreate(Activity.java:7023)
            at android.app.Activity.performCreate(Activity.java:7014)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1215)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2734)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2859)
            at android.app.ActivityThread.-wrap11(Unknown Source:0)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1592)
            at android.os.Handler.dispatchMessage(Handler.java:106)
            at android.os.Looper.loop(Looper.java:164)
            at android.app.ActivityThread.main(ActivityThread.java:6518)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
         Caused by: android.content.res.Resources$NotFoundException: Drawable ca.company.project.betaversion:color/nav_bgstate_list with resource ID #0x7f0600d9
         Caused by: android.content.res.Resources$NotFoundException: File res/color/nav_bgstate_list.xml from drawable resource ID #0x7f0600d9
            at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:820)
            at android.content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:630)
            at android.content.res.Resources.getDrawableForDensity(Resources.java:877)
            at android.content.res.Resources.getDrawable(Resources.java:819)
            at android.content.Context.getDrawable(Context.java:605)
            at androidx.core.content.ContextCompat.getDrawable(ContextCompat.java:455)
            at androidx.appcompat.widget.ResourceManagerInternal.getDrawable(ResourceManagerInternal.java:147)
            at androidx.appcompat.widget.ResourceManagerInternal.getDrawable(ResourceManagerInternal.java:135)
    2021-06-09 07:49:13.569 12895-12895/ca.company.project.betaversion E/AndroidRuntime:     at androidx.appcompat.content.res.AppCompatResources.getDrawable(AppCompatResources.java:104)
            at androidx.appcompat.widget.TintTypedArray.getDrawable(TintTypedArray.java:83)
            at com.google.android.material.navigation.NavigationView.<init>(NavigationView.java:196)
            at com.google.android.material.navigation.NavigationView.<init>(NavigationView.java:122)
            at java.lang.reflect.Constructor.newInstance0(Native Method)
            at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
            at android.view.LayoutInflater.createView(LayoutInflater.java:647)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
            at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
            at ca.company.project.betaversion.databinding.ActivityMainBinding.inflate(ActivityMainBinding.java:57)
            at ca.company.project.betaversion.databinding.ActivityMainBinding.inflate(ActivityMainBinding.java:51)
            at ca.company.project.betaversion.MainActivity.onCreate(MainActivity.kt:111)
            at android.app.Activity.performCreate(Activity.java:7023)
            at android.app.Activity.performCreate(Activity.java:7014)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1215)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2734)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2859)
            at android.app.ActivityThread.-wrap11(Unknown Source:0)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1592)
            at android.os.Handler.dispatchMessage(Handler.java:106)
            at android.os.Looper.loop(Looper.java:164)
            at android.app.ActivityThread.main(ActivityThread.java:6518)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
         Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #4: <item> tag requires a 'drawable' attribute or child tag defining a drawable
            at android.graphics.drawable.StateListDrawable.inflateChildElements(StateListDrawable.java:189)
            at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:122)
            at android.graphics.drawable.DrawableInflater.inflateFromXmlForDensity(DrawableInflater.java:142)
            at android.graphics.drawable.Drawable.createFromXmlInnerForDensity(Drawable.java:1295)
            at android.graphics.drawable.Drawable.createFromXmlForDensity(Drawable.java:1254)
            at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:807)
                ... 36 more



我检查了 minSdk 版本、编译的 sdk 版本、主题、gradle 版本、依赖关系及其版本等,一切都是一样的,没有什么不同。我知道我应该使用可绘制状态而不是颜色状态,但我不明白为什么它在其他项目中起作用,但在另一个项目中起作用。

编辑 1

我发现发生崩溃是因为我使用颜色状态选择器设置了 itembackground。itemIconTint 和 itemTextColor 适用于颜色状态选择器。每当我尝试在那里设置状态选择器时,只有 itemBackground 崩溃,所以我找到了这里提到的一个解决方案,基本上我需要添加检查 android:checkable="true",但即使在添加它之后它也会崩溃。我不知道为什么。

编辑 2

所以我意识到颜色状态选择器适用于较新版本的 android > 10,但不适用于旧版本的 android。

4

0 回答 0