KitKat及以下的VectorDrawableCompat Resources $ NotFoundException

时间:2016-03-03 16:11:46

标签: android android-appcompat android-vectordrawable

启用时

vectorDrawables.useSupportLibrary = true

我在KitKat和以下设备上获得了很多resourceNotFoundExceptions。我得到了这个用于NavigationView for Menus以及用于带有app:srcCompat

的ImageViews
android.content.res.Resources$NotFoundException: Resource ID #0x7f02006b
at android.content.res.Resources.getValue(Resources.java:2305)
at android.support.v7.widget.AppCompatDrawableManager.loadDrawableFromDelegates(AppCompatDrawableManager.java:268)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:178)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:173)
at android.support.v7.view.menu.MenuItemImpl.getIcon(MenuItemImpl.java:421)
at android.support.v7.view.menu.ActionMenuItemView.initialize(ActionMenuItemView.java:114)
at android.support.v7.widget.ActionMenuPresenter.bindItemView(ActionMenuPresenter.java:206)
at android.support.v7.view.menu.BaseMenuPresenter.getItemView(BaseMenuPresenter.java:182)
at android.support.v7.widget.ActionMenuPresenter.getItemView(ActionMenuPresenter.java:192)
at  android.support.v7.widget.ActionMenuPresenter.flagActionItems(ActionMenuPresenter.java:480)
at android.support.v7.view.menu.MenuBuilder.flagActionItems(MenuBuilder.java:1138)
at android.support.v7.view.menu.BaseMenuPresenter.updateMenuView(BaseMenuPresenter.java:91)
at android.support.v7.widget.ActionMenuPresenter.updateMenuView(ActionMenuPresenter.java:229)
at android.support.v7.view.menu.MenuBuilder.dispatchPresenterUpdate(MenuBuilder.java:284)
at android.support.v7.view.menu.MenuBuilder.onItemsChanged(MenuBuilder.java:1030)
at android.support.v7.view.menu.MenuBuilder.startDispatchingItemsChanged(MenuBuilder.java:1053)
at android.support.v7.app.AppCompatDelegateImplV7.preparePanel(AppCompatDelegateImplV7.java:1345)
at android.support.v7.app.AppCompatDelegateImplV7.doInvalidatePanelMenu(AppCompatDelegateImplV7.java:1583)
at android.support.v7.app.AppCompatDelegateImplV7.access$100(AppCompatDelegateImplV7.java:89)
at android.support.v7.app.AppCompatDelegateImplV7$1.run(AppCompatDelegateImplV7.java:128)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5653)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:110)

用于NavigationView

Process: org.ligi.gobandroid_hd, PID: 11473
java.lang.RuntimeException: Unable to start activity ComponentInfo{org.ligi.gobandroid_hd/org.ligi.gobandroid_hd.ui.game_setup.GoSetupActivity}: android.view.InflateException: Binary XML file line #20: Error inflating class android.support.design.widget.NavigationView
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2404)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2464)
    at android.app.ActivityThread.access$900(ActivityThread.java:172)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:146)
    at android.app.ActivityThread.main(ActivityThread.java:5653)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
    at dalvik.system.NativeStart.main(Native Method)
 Caused by: android.view.InflateException: Binary XML file line #20: Error inflating class android.support.design.widget.NavigationView
    at android.view.LayoutInflater.createView(LayoutInflater.java:626)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:702)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
    at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:267)
    at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:129)
    at org.ligi.gobandroid_hd.ui.application.GobandroidFragmentActivity.setContentView(GobandroidFragmentActivity.java:85)
    at org.ligi.gobandroid_hd.ui.GoActivity.onCreate(GoActivity.java:108)
    at android.app.Activity.performCreate(Activity.java:5541)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2368)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2464) 
    at android.app.ActivityThread.access$900(ActivityThread.java:172) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:146) 
    at android.app.ActivityThread.main(ActivityThread.java:5653) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:515) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107) 
    at dalvik.system.NativeStart.main(Native Method) 
 Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Constructor.constructNative(Native Method)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at android.view.LayoutInflater.createView(LayoutInflater.java:600)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:702) 
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:761) 
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:769) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:498) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:398) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:354) 
    at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:267) 
    at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:129) 
    at org.ligi.gobandroid_hd.ui.application.GobandroidFragmentActivity.setContentView(GobandroidFragmentActivity.java:85) 
    at org.ligi.gobandroid_hd.ui.GoActivity.onCreate(GoActivity.java:108) 
    at android.app.Activity.performCreate(Activity.java:5541) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2368) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2464) 
    at android.app.ActivityThread.access$900(ActivityThread.java:172) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:146) 
    at android.app.ActivityThread.main(ActivityThread.java:5653) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:515) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107) 
    at dalvik.system.NativeStart.main(Native Method) 
 Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f020067
    at android.content.res.Resources.getValue(Resources.java:2305)
    at android.support.v7.widget.AppCompatDrawableManager.loadDrawableFromDelegates(AppCompatDrawableManager.java:268)
    at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:178)
    at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:173)
    at android.support.v7.view.menu.MenuItemImpl.getIcon(MenuItemImpl.java:421)
    at android.support.design.internal.NavigationMenuPresenter$NavigationMenuAdapter.prepareMenuItems(NavigationMenuPresenter.java:475)
    at android.support.design.internal.NavigationMenuPresenter$NavigationMenuAdapter.update(NavigationMenuPresenter.java:436)
    at android.support.design.internal.NavigationMenuPresenter.updateMenuView(NavigationMenuPresenter.java:112)
    at android.support.design.widget.NavigationView.inflateMenu(NavigationView.java:241)
    at android.support.design.widget.NavigationView.<init>(NavigationView.java:169)
    at android.support.design.widget.NavigationView.<init>(NavigationView.java:95)
    at java.lang.reflect.Constructor.constructNative(Native Method) 
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423) 
    at android.view.LayoutInflater.createView(LayoutInflater.java:600) 
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:702) 
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:761) 
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:769) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:498) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:398) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:354) 
    at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:267) 
    at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:129) 
    at org.ligi.gobandroid_hd.ui.application.GobandroidFragmentActivity.setContentView(GobandroidFragmentActivity.java:85) 
    at org.ligi.gobandroid_hd.ui.GoActivity.onCreate(GoActivity.java:108) 
    at android.app.Activity.performCreate(Activity.java:5541) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2368) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2464) 
    at android.app.ActivityThread.access$900(ActivityThread.java:172) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:146) 
    at android.app.ActivityThread.main(ActivityThread.java:5653) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:515) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107) 
    at dalvik.system.NativeStart.main(Native Method) 

9 个答案:

答案 0 :(得分:32)

要支持Lollipop之前的版本,请使用

  

com.android.support:appcompat-v7:24.0.0(或更高版本)

支持图书馆。

然后,而不是

ContextCompat.getDrawable(view.getContext(), id);

使用这个

AppCompatResources.getDrawable(view.getContext(), id);

答案 1 :(得分:27)

我发现了问题:我在drawable-anydpi /中使用了vector-drawables - 这会导致崩溃 - 当我将drawables移动到drawable /它工作正常时

答案 2 :(得分:5)

通过将它们包装在StateListDrawable(选择器)中,我能够在 pre-Lollipop 设备上使用矢量drawable。

更确切地说,我创建了一个XML格式的选择器,然后在其中添加了单个矢量drawable

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/vector_ic_action_add" />
</selector>

然后我在菜单中使用这个“包裹”的drawable。如果您想了解有关其工作原理的更多信息,请查看Chris Banes撰写的this非常有用的文章。

答案 3 :(得分:4)

据我所知,文档中只有ImageView的XML访问权限。

对于您必须在java中使用的菜单。

toolbar.inflateMenu(menuResId);
toolbar.getMenu()
       .findItem(menuItemId)
       .setIcon(VectorDrawableCompat
                       .createFromResource(resource, drawableId);

答案 4 :(得分:2)

要解决此问题,我必须在应用程序启动时添加以下代码(Application子类):

AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);

除了:

  • 在布局定义中使用app:srcCompat(在我的情况下为ImageButton视图),而不是android:src
  • 激活gradle:vectorDrawables.useSupportLibrary = true

答案 5 :(得分:0)

如果您正在使用vectorDrawableComapt并将其设置为XML,请记住以正确的方式执行此操作:

app:srcCompat="@drawable/ic_add"

来源: https://android-developers.blogspot.co.uk/2016/02/android-support-library-232.html

答案 6 :(得分:0)

我遇到了同样的问题,但是浪费了一个小时才解决了问题

  1. 删除可绘制对象以确保只有可绘制对象
  2. 在build.gradle中使用

    defaultConfig {vectorDrawables.useSupportLibrary = true}

    1. 设置可绘制到ImageView的向量,如下所示
       <ImageView android:id="@+id/ivLogo" android:layout_width="100dp" android:layout_height="100dp" android:background="@drawable/your_selector" app:srcCompat="@drawable/ic_your_vector_logo" />

    2. 您的活动必须扩展AppCompatActiviity而不是Activity

希望您的问题得到解决。

答案 7 :(得分:0)

对我来说,我正在为样式 <ContentPage.Content> <ScrollView> <ListView x:Name="elo" Margin="10,0" RowHeight="250"> <ListView.ItemTemplate> <DataTemplate> <ViewCell > <Grid > <StackLayout > <Label Text="{Binding Question1}" /> <Button Clicked="indexOne" Text="{Binding Answers[0]}" /> <Button Clicked="indexTwo" Text="{Binding Answers[1]}" /> <Button Clicked="indexThree" Text="{Binding Answers[2]}"/> <Button Clicked="indexFour" Text="{Binding Answers[3]}" /> </StackLayout> </Grid> </ViewCell> </DataTemplate> </ListView.ItemTemplate> </ListView> </ScrollView> </ContentPage.Content> 使用纯色的样式参考。它在KitKat上崩溃,但不在上面。已更新为使用实际的颜色代码,但它消失了。 ?colorAccent

答案 8 :(得分:0)

一个问题可能是媒介本身。我的一条路长约4k个字符,尽管有警告我只是忽略了它。尽管预览效果很好,但在运行时会崩溃。