LayerDrawable

时间:2017-04-17 07:36:42

标签: android android-support-library layerdrawable

我使用Android支持库版本23.2.1的Android应用程序,一切正常。但是,昨天我已经将android支持库版本更新为25.2.0并且意识到一些用户正在获得此异常(通过Fabric crashlytics)但我无法重现它。

以下是崩溃日志:

Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.drawable.Drawable android.graphics.drawable.Drawable$ConstantState.newDrawable(android.content.res.Resources)' on a null object reference
   at android.graphics.drawable.LayerDrawable$ChildDrawable.<init>(LayerDrawable.java:959)
   at android.graphics.drawable.LayerDrawable$LayerState.<init>(LayerDrawable.java:1007)
   at android.graphics.drawable.LayerDrawable.createConstantState(LayerDrawable.java:133)
   at android.graphics.drawable.LayerDrawable.<init>(LayerDrawable.java:126)
   at android.graphics.drawable.LayerDrawable$LayerState.newDrawable(LayerDrawable.java:1048)
   at android.content.res.Resources.getCachedDrawableLocked(Resources.java:2777)
   at android.content.res.Resources.getCachedDrawable(Resources.java:2745)
   at android.content.res.Resources.loadDrawable(Resources.java:2586)
   at android.content.res.Resources.getDrawable(Resources.java:900)
   at android.content.Context.getDrawable(Context.java:408)
   at android.support.v4.content.ContextCompatApi21.getDrawable(ContextCompatApi21.java:30)
   at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:372)
   at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:202)
   at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:190)
   at android.support.v7.content.res.AppCompatResources.getDrawable(AppCompatResources.java:100)
   at android.support.v7.view.menu.MenuItemImpl.getIcon(MenuItemImpl.java:425)
   at android.support.v7.view.menu.ActionMenuItemView.initialize(ActionMenuItemView.java:129)
   at android.support.v7.widget.ActionMenuPresenter.bindItemView(ActionMenuPresenter.java:211)
   at android.support.v7.view.menu.BaseMenuPresenter.getItemView(BaseMenuPresenter.java:186)
   at android.support.v7.widget.ActionMenuPresenter.getItemView(ActionMenuPresenter.java:197)
   at android.support.v7.widget.ActionMenuPresenter.flagActionItems(ActionMenuPresenter.java:479)
   at android.support.v7.view.menu.MenuBuilder.flagActionItems(MenuBuilder.java:1160)
   at android.support.v7.view.menu.BaseMenuPresenter.updateMenuView(BaseMenuPresenter.java:95)
   at android.support.v7.widget.ActionMenuPresenter.updateMenuView(ActionMenuPresenter.java:234)
   at android.support.v7.view.menu.MenuBuilder.dispatchPresenterUpdate(MenuBuilder.java:292)
   at android.support.v7.view.menu.MenuBuilder.onItemsChanged(MenuBuilder.java:1047)
   at android.support.v7.view.menu.MenuBuilder.startDispatchingItemsChanged(MenuBuilder.java:1074)
   at android.support.v7.app.ToolbarActionBar.populateOptionsMenu(ToolbarActionBar.java:460)
   at android.support.v7.app.ToolbarActionBar$1.run(ToolbarActionBar.java:61)
   at android.os.Handler.handleCallback(Handler.java:815)
   at android.os.Handler.dispatchMessage(Handler.java:104)
   at android.os.Looper.loop(Looper.java:194)
   at android.app.ActivityThread.main(ActivityThread.java:5637)
   at java.lang.reflect.Method.invoke(Method.java)
   at java.lang.reflect.Method.invoke(Method.java:372)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)

它似乎与菜单视图有关。菜单选项的布局是:

<?xml version="1.0" encoding="utf-8"?>
<menu 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"
    tools:context=".tablet.ui.navimenu.ContainerActivity">

    <item
        android:id="@+id/memo_fragment"
        android:icon="@drawable/ic_class_white_24dp"
        android:orderInCategory="100"
        android:title="@string/memo_fragment"
        app:showAsAction="always" />

    <item
        android:id="@+id/notification_fragment"
        android:icon="@drawable/layerlist_menu_notification"
        android:orderInCategory="200"
        android:title="@string/notification_fragment"
        app:showAsAction="always" />

</menu>

layerlist_menu_notification布局是一个带徽章的图标:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/ic_notification"
        android:drawable="@drawable/ic_notifications_white_24dp"
        android:gravity="center" />

    <!-- set a place holder Drawable so android:drawable isn't null -->
    <item
        android:id="@+id/ic_badge"
        android:drawable="@drawable/ic_notifications_white_24dp" />
</layer-list>

知道哪里出错了?

1 个答案:

答案 0 :(得分:0)

尝试在build.gradle中更改以下内容

 compileSdkVersion 25
buildToolsVersion "25.0.2"
compile 'com.android.support:appcompat-v7:25.1.0'
compile 'com.android.support:design:25.1.0'
compile 'com.android.support:support-v4:25.1.0'