Android Resources$NotFoundException from drawable resource for older phones API 18
Asked Answered
P

3

5

I'm running into the following error only when testing on a Samsung Galaxy Nexus phone with Android 4.3 and API 18.

Caused by: android.content.res.Resources$NotFoundException: File res/drawable/ic_error.xml
 from drawable resource ID #0x7f020098
    at android.content.res.Resources.loadDrawable(Resources.java:2091)
    at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
    at android.widget.TextView.<init>(TextView.java:803)
    at android.widget.EditText.<init>(EditText.java:60)

In my Acitivty.java file, I've tried using the following methods to set my vector drawable:

editTextNickname.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_error, 0);
editTextNickname.setCompoundDrawables(null, null,
    ContextCompat.getDrawable(getApplicationContext(), R.drawable.ic_error), null);

I'm using the latest support libraries in my build.gradle file, as shown below:

apply plugin: 'com.google.gms.google-services'
apply plugin: 'com.android.application'

android {
  compileSdkVersion 24
  buildToolsVersion "23.0.3"

defaultConfig {
  applicationId "com.peprally.jeremy.peprally"
  minSdkVersion 16
  targetSdkVersion 24
  versionCode 1
  versionName "1.0"
  vectorDrawables.useSupportLibrary = true
}
buildTypes {
  release {
    minifyEnabled false
    proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
  }
  debug {
    debuggable true
  }
}

repositories {
  mavenCentral()
}

dependencies {
  compile fileTree(dir: 'libs', include: ['*.jar'])
  compile 'com.android.support:appcompat-v7:24.1.1'
  compile 'com.android.support:design:24.1.1'
  compile 'com.android.support:cardview-v7:24.1.1'
  compile 'com.android.support:support-v4:24.1.1'
  compile 'com.android.support:support-vector-drawable:24.1.1'
  compile 'com.facebook.android:facebook-android-sdk:4.6.0'
  compile 'com.google.android.gms:play-services-appindexing:9.2.1'
  compile 'com.google.firebase:firebase-core:9.2.1'
  compile 'com.google.firebase:firebase-messaging:9.2.1'
  compile 'com.amazonaws:aws-android-sdk-core:2.+'
  compile 'com.amazonaws:aws-android-sdk-cognito:2.+'
  compile 'com.amazonaws:aws-android-sdk-s3:2.+'
  compile 'com.amazonaws:aws-android-sdk-ddb:2.+'
  compile 'com.amazonaws:aws-android-sdk-ddb-mapper:2.+'
  compile 'com.squareup.picasso:picasso:2.5.2'
  compile 'com.android.volley:volley:1.0.0'
  compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha1'
  testCompile 'junit:junit:4.12'
}

apply plugin: 'com.google.gms.google-services'

I've seen similar problems being posted on StackOverflow but no good solutions yet. If anyone has any insights, please help! Thanks!

<--EDIT-->

After ignoring the issue and coming back to it, I found a solid solution to get around the VectorDrawable not supported issue. On top of what was suggested, you can create VectorDrawables using the code below:

public static Drawable getAPICompatVectorDrawable(Context callingContext, int resource_id) {
  if (android.os.Build.VERSION.SDK_INT >= 21) {
    return ContextCompat.getDrawable(callingContext.getApplicationContext(), resource_id);
  } else {
    return VectorDrawableCompat.create(
        callingContext.getResources(),
        resource_id,
        callingContext.getTheme());
  }
}

I also had issues trying to put vector drawables directly into xml files. And I couldn't find a way to get around that so I eventually just took them out of my xml files and injected them programmatically in java using the same function as above. The error I got from using vector drawables inside my xml is shown below, if anyone has a better solution, I would love to hear it!

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.
  peprally.jeremy.peprally/com.peprally.jeremy.peprally.activities.
  NewCommentActivity}:
android.view.InflateException: Binary XML file line #90: Error inflating class TextView
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
    at android.app.ActivityThread.access$600(ActivityThread.java:141)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5103)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:525)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #90:
  Error inflating class TextView
Polycrates answered 30/7, 2016 at 17:54 Comment(0)
C
7

Could you please test the code below? Just to see if works... Then, I'll update the answer with more information

public class MainActivity extends AppCompatActivity {
    static {
        AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
    }

    @Override
    protected void onCreate(Bundle arg0) {
        ...
    }
    ...
}

Background

I was aware that due to this Google Issue - 205236, support to vector Drawable was disabled due to Memory Issues.

One of the comments in that issue is that:

To fix this issue, support for VectorDrawable from resource XML had to be removed.

And then,

In the next release I've added an opt-in API where you can re-enable the VectorDrawable support which was removed.

So, they re-enabled that option in
Android Support Library 23.4.0

For AppCompat users, we’ve added an opt-in API to re-enable support Vector Drawables from resources (the behavior found in 23.2) via AppCompatDelegate.setCompatVectorFromResourcesEnabled(true) - keep in mind that this still can cause issues with memory usage and problems updating Configuration instances, hence why it is disabled by default.

Maybe, build.gradle setting is now obsolete and you just need to enable it in proper activities (however, need to test).

Cargill answered 30/7, 2016 at 18:6 Comment(9)
omg yes, that worked! please elaborate! And thank you so much for the tip, I've been searching for days for a solution!Polycrates
but moreover, I'm running into more compatibility problems with other UI components such as FloatingActionButtons.Polycrates
Maybe I misunderstand, but doesn't this Gradle setting do something similar? vectorDrawables.useSupportLibrary = trueLineal
@Polycrates Updated :)Cargill
@cricket_007 I'm not sure.. but I guess that build.gradle setting was used only in the first time that they enabled vector drawable. After remove and re-enabled, I guess it is now obsolete... but I need to testCargill
hey @GuilhermeP, thanks for the explanation! I'm still having trouble using vector drawables statically in my layout files and inflating layouts. I tried calling AppCompatDelegate.setCompatVectorFromResourcesEnabled(true); before inflating any layouts that use vector drawables but I get an error saying "Error inflating class ..." and not much else. Any suggestions>Polycrates
@Polycrates Was Resources$NotFoundException error fixed? Is it a new error? I updated the code.. Note that you must insert that line before any method. If possible, share the whole new logcat for the new error (or move to another question if the error is different)...Cargill
@GuilhermeP it is a new error but I believe of the same root cause - the fact that vectordrawables are not supported in API versions < 23. I will share the logcat of the new error but I also found a temporary fix to get around these issues so that my app doesn't crash at least. Please see my edit ^Polycrates
For me worked !! Thank you !! It solved my problem with API 19- and 16+.Falsework
C
0

About AppCompatDelegate: "This feature defaults to disabled, since enabling it can cause issues with memory usage, and problems updating Configuration instances. If you update the configuration manually, then you probably do not want to enable this. You have been warned.

Even with this disabled, you can still use vector resources through setImageResource(int) and its app:srcCompat attribute. They can also be used in anything which AppCompat inflates for you, such as menu resources." source

Cavie answered 7/6, 2018 at 9:8 Comment(0)
P
0

it's not a preferred solution but working, I mix vector drawable and PNG from this website.

Phanerogam answered 29/7, 2021 at 12:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.