来自 LayerDrawable 的异常
Posted
技术标签:
【中文标题】来自 LayerDrawable 的异常【英文标题】:Exception from LayerDrawable 【发布时间】:2017-09-12 19:43:39 【问题描述】:我有一个使用 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>
知道哪里出错了吗?
【问题讨论】:
你在使用矢量图吗? @rafsanahmad007 是的,我已经用我的布局代码更新了帖子,是的,我正在使用矢量绘图。 针对同一问题的任何解决方案 还有任何解决方法@Bryanleesh 【参考方案1】:尝试在 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'
【讨论】:
25.2.0
是不是有 bug?以上是关于来自 LayerDrawable 的异常的主要内容,如果未能解决你的问题,请参考以下文章
LayerDrawable OutOfMemoryError
自定义 LayerDrawable:onStateChange 从未调用