ligi
ligi

Reputation: 39529

VectorDrawableCompat Resources$NotFoundException on KitKat and below

When I enable

vectorDrawables.useSupportLibrary = true

I get a lot of resourceNotFoundExceptions on KitKat and below devices. I get this for NavigationView for Menus and also for ImageViews with app:srcCompat

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)

for 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) 

Upvotes: 18

Views: 12923

Answers (9)

Daniel Wilson
Daniel Wilson

Reputation: 19824

One issue could be the vector itself. One of my paths was like 4k characters long and although there was a warning I just ignored it. Although it was previewing fine, it would crash at run time.

Upvotes: 0

dahis39
dahis39

Reputation: 15

For me, I was using style reference for solid color ?colorAccent for a shape. It crash on KitKat but not above. Updated to use actual color code and it goes away. <solid android:color="#7885cb" />

Upvotes: 0

Ness Tyagi
Ness Tyagi

Reputation: 2028

I faced same issue but after some hour of wasting solved the issue

  1. Remove drawables make sure just have drawable
  2. In build.gradle use

    defaultConfig {vectorDrawables.useSupportLibrary = true}

    1. Make set vector drawable to ImageView as below
      <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. Your activity must extends AppCompatActiviity instead of Activity

I hope your problem solved.

Upvotes: 0

Virtual
Virtual

Reputation: 459

To fix this issue, I had to add the following code at the application startup (Application subclass) :

AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);

In addition to :

  • Use app:srcCompat in layout definition (ImageButton view in my case) instead of android:src.
  • Activate in gradle : vectorDrawables.useSupportLibrary = true

Upvotes: 5

Rakshith Kumar
Rakshith Kumar

Reputation: 869

To support versions before Lollipop, use

com.android.support:appcompat-v7:24.0.0 (or later)

support library.

And then, instead of

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

use this one

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

Upvotes: 36

Clive Jefferies
Clive Jefferies

Reputation: 1158

If you are using vectorDrawableComapt and setting it in XML remember to do it the correct way:

app:srcCompat="@drawable/ic_add"

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

Upvotes: 0

ligi
ligi

Reputation: 39529

I found the problem: I had my vector-drawables in drawable-anydpi/ - this produces this crash - when i move the drawables to drawable/ it works fine

Upvotes: 32

Alexandre Piveteau
Alexandre Piveteau

Reputation: 203

I was able to use the vector drawables on pre-Lollipop devices by wrapping them in a StateListDrawable (a selector).

More precisely, I created a selector drawable in XML, and then added a single vector drawable in them :

<?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>

I then use this "wrapped" drawable in my menu. If you want more information about how it works, check out this very useful post by Chris Banes.

Upvotes: 5

Budius
Budius

Reputation: 39836

as far as I've read on the docs there's only XML access for ImageView.

For the Menu you'll have to use in java.

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

Upvotes: 4

Related Questions