BottomNavigationView膨胀错误。 Resources $ NotFoundException:资源ID#0x0

时间:2018-10-20 12:21:44

标签: android android-support-library bottomnavigationview android-support-design

我在设计支持库中的BottomNavigationView遇到问题。 我在我的activity_main.xml中使用它:

<android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        app:itemIconSize="16dp"
        app:labelVisibilityMode="labeled"
        app:itemTextColor="@color/white"
        app:itemIconTint="@color/white"
        app:itemBackground="@color/medium_purple"
        app:menu="@menu/main"
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

膨胀该xml时,应用程序崩溃。异常的完整堆栈跟踪如下:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.frolo.musp.dev/com.alexfrolov.mp.main.MainActivity}: android.view.InflateException: Binary XML file line #16: Binary XML file line #16: Error inflating class android.support.design.widget.BottomNavigationView
                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2814)
                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
                                                                        at android.app.ActivityThread.-wrap11(Unknown Source:0)
                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1613)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:106)
                                                                        at android.os.Looper.loop(Looper.java:164)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:6651)
                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:822)
                                                                     Caused by: android.view.InflateException: Binary XML file line #16: Binary XML file line #16: Error inflating class android.support.design.widget.BottomNavigationView
                                                                     Caused by: android.view.InflateException: Binary XML file line #16: Error inflating class android.support.design.widget.BottomNavigationView
                                                                     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:651)
                                                                        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:794)
                                                                        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:734)
                                                                        at android.view.LayoutInflater.rInflate(LayoutInflater.java:867)
                                                                        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:828)
                                                                        at android.view.LayoutInflater.inflate(LayoutInflater.java:519)
                                                                        at android.view.LayoutInflater.inflate(LayoutInflater.java:427)
                                                                        at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
                                                                        at android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
                                                                        at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
                                                                        at com.alexfrolov.mp.main.MainActivity.onCreate(MainActivity.java:165)
                                                                        at android.app.Activity.performCreate(Activity.java:7074)
                                                                        at android.app.Activity.performCreate(Activity.java:7065)
                                                                        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1215)
                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2767)
                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
                                                                        at android.app.ActivityThread.-wrap11(Unknown Source:0)
                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1613)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:106)
                                                                        at android.os.Looper.loop(Looper.java:164)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:6651)
                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:822)
                                                                     Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x0
                                                                        at android.content.res.ResourcesImpl.getValue(ResourcesImpl.java:220)
                                                                        at android.content.res.MiuiResourcesImpl.getValue(MiuiResourcesImpl.java:93)
                                                                        at android.content.res.Resources.getColorStateList(Resources.java:1034)
                                                                        at android.content.Context.getColorStateList(Context.java:631)
                                                                        at android.support.v7.content.res.AppCompatResources.getColorStateList(AppCompatResources.java:67)
                                                                        at android.support.design.internal.BottomNavigationMenuView.createDefaultColorStateList(BottomNavigationMenuView.java:467)
                                                                        at android.support.design.internal.BottomNavigationMenuView.<init>(BottomNavigationMenuView.java:101)
                                                                        at android.support.design.internal.BottomNavigationMenuView.<init>(BottomNavigationMenuView.java:86)

所有支持库的版本和compileSdk的版本相同。我真的不明白那是什么鬼。有人可以解释一下吗?

谢谢

2 个答案:

答案 0 :(得分:3)

我遇到了这个问题,这使我困惑了一天左右。基本上,BottomNav希望在主题中为textColorSecondary定义一个colorstatelist,而不是纯色。所以我从:

ScrollPalyerView.setOnScrollChangeListener(new View.OnScrollChangeListener() {
        @Override
        public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {

            if (scrollY==`Max scrollY`)
               {
                  Log("tag","end of scrollView")
                }
        }
    });

<style name="MTheme" parent="Theme.MaterialComponents.Light.Bridge">
    ...   
    <item name="android:textColorSecondary">#295055</item>

其中secondary_textcolor等于

<style name="MTheme" parent="Theme.MaterialComponents.Light.Bridge">
    ...
    <item name="android:textColorSecondary">@color/secondary_textcolor</item>

我浪费了大量时间来添加和删除依赖项,以找到这种解决方案。

答案 1 :(得分:0)

问题出在您的问题标题中

Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x0

这意味着您正在使用的资源不存在。我不知道这是什么,但是这就是您要解决的方法。转到文件@menu/main并删除图标的所有属性。您可以保留文本。逐一添加以检查哪个是问题所在。最有可能是您的图标被错误地放置在文件夹drawable-24中,而不是drawable或比您要测试的文件夹更高的android API文件夹中。有时我们会犯那些错误。

还请注意,某些属性仅适用于从某个Android API开始的内容。考虑检查以app开头的属性。

    app:itemTextColor="@color/white"
    app:itemIconTint="@color/white"
    app:itemBackground="@color/medium_purple"