Quand j'active
vectorDrawables.useSupportLibrary = true
Je reçois beaucoup d'exceptions resourceNotFoundExceptions sur les périphériques KitKat et inférieurs. Je reçois ceci pour NavigationView pour les menus et aussi pour ImageViews avec 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)
pour 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)
J'ai trouvé le problème: j'avais mon dessin vectoriel dans drawable-anydpi/- cela produit ce crash - quand je déplace les drawables vers drawable/cela fonctionne très bien
Pour prendre en charge les versions antérieures à Lollipop, utilisez
com.Android.support:appcompat-v7:24.0.0 (ou version ultérieure)
bibliothèque de support.
Et puis, au lieu de
ContextCompat.getDrawable(view.getContext(), id);
utiliser celui-ci
AppCompatResources.getDrawable(view.getContext(), id);
J'ai été en mesure d'utiliser le vecteur dessinable sur pre-Lollipop devices en les enveloppant dans un StateListDrawable (un sélecteur).
Plus précisément, j'ai créé un sélecteur pouvant être dessiné en XML, puis ajouté un vecteur unique pouvant être dessiné dans ceux-ci:
<?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>
J'utilise ensuite ce dessin "emballé" dans mon menu. Si vous souhaitez plus d'informations sur son fonctionnement, consultez this post très utile de Chris Banes.
autant que j'ai lu sur la documentation, il n'y a qu'un accès XML pour ImageView.
Pour le menu, vous devrez utiliser en Java.
toolbar.inflateMenu(menuResId);
toolbar.getMenu()
.findItem(menuItemId)
.setIcon(VectorDrawableCompat
.createFromResource(resource, drawableId);
Pour résoudre ce problème, j'ai dû ajouter le code suivant au démarrage de l'application (sous-classe Application):
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
En plus de :
app:srcCompat
dans la définition de la présentation (la vue ImageButton dans mon cas) au lieu de Android:src
.vectorDrawables.useSupportLibrary = true
J'ai rencontré le même problème, mais après une heure de gaspillage résolu le problème
Dans build.gradle use
defaultConfig {vectorDrawables.useSupportLibrary = true}
Rendre le vecteur dessinable à ImageView comme ci-dessous<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" />
Votre activité doit étendre AppCompatActiviity au lieu de Activity
J'espère que ton problème est résolu.
Si vous utilisez vectorDrawableComapt et le définissez en XML, n'oubliez pas de le faire correctement:
app:srcCompat="@drawable/ic_add"
Source: https://Android-developers.blogspot.co.uk/2016/02/Android-support-library-232.html