Resources$NotFoundException: Drawable com.example.test:drawable/back_button with resource ID #0x7f080073

▼魔方 西西 提交于 2021-02-11 15:20:49

问题


I recently added vector drawables (anydpi) to my android studio project in addition to png images (mdpi, hdpi, xhdpi, and xxhdpi)--android studio does this by default now.

I immediately started getting this error:

2019-02-10 23:10:25.870 2722-2722/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.test, PID: 2722
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.SettingsActivity}: android.content.res.Resources$NotFoundException: Drawable com.example.test:drawable/back_button with resource ID #0x7f080073
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2951)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3086)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1816)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6718)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
     Caused by: android.content.res.Resources$NotFoundException: Drawable com.example.test:drawable/back_button with resource ID #0x7f080073
     Caused by: android.content.res.Resources$NotFoundException: File res/drawable/back_button.xml from drawable resource ID #0x7f080073
        at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:847)
        at android.content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:631)
        at android.content.res.Resources.getDrawableForDensity(Resources.java:888)
        at android.content.res.Resources.getDrawable(Resources.java:827)
        at android.content.Context.getDrawable(Context.java:635)
        at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:463)
        at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:203)
        at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:191)
        at android.support.v7.content.res.AppCompatResources.getDrawable(AppCompatResources.java:102)
        at android.support.v7.widget.ToolbarWidgetWrapper.setNavigationIcon(ToolbarWidgetWrapper.java:599)
        at android.support.v7.app.ToolbarActionBar.setHomeAsUpIndicator(ToolbarActionBar.java:161)
        at com.example.test.SettingsActivity.setUpToolbarAndActionBar(SettingsActivity.java:642)
        at com.example.test.SettingsActivity.onCreate(SettingsActivity.java:159)
        at android.app.Activity.performCreate(Activity.java:7144)
        at android.app.Activity.performCreate(Activity.java:7135)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2931)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3086)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1816)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6718)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
     Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #4: <bitmap> requires a valid 'src' attribute
        at android.graphics.drawable.BitmapDrawable.updateStateFromTypedArray(BitmapDrawable.java:849)
        at android.graphics.drawable.BitmapDrawable.inflate(BitmapDrawable.java:770)
2019-02-10 23:10:25.870 2722-2722/? E/AndroidRuntime:     at android.graphics.drawable.DrawableInflater.inflateFromXmlForDensity(DrawableInflater.java:142)
        at android.graphics.drawable.Drawable.createFromXmlInnerForDensity(Drawable.java:1332)
        at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:1321)
        at android.graphics.drawable.StateListDrawable.inflateChildElements(StateListDrawable.java:194)
        at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:122)
        at android.graphics.drawable.DrawableInflater.inflateFromXmlForDensity(DrawableInflater.java:142)
        at android.graphics.drawable.Drawable.createFromXmlInnerForDensity(Drawable.java:1332)
        at android.graphics.drawable.Drawable.createFromXmlForDensity(Drawable.java:1291)
        at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:833)
            ... 27 more

The error led to here in my code:

Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar_settings);
setSupportActionBar(toolbar);

ActionBar actionBar = getSupportActionBar();

actionBar.setDisplayHomeAsUpEnabled(true);
int drawable = R.drawable.back_button;
actionBar.setHomeAsUpIndicator(drawable); // ERROR IS HERE
toolbar.setNavigationIcon(drawable);

The code above is called in the onCreate of my activity, where I set a custom drawable (R.drawable.back_button) to the back button of the activity.

However, R.drawable.back_button is a drawable resource file which changes the drawable when it is pressed:

<selector
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="false">
        <bitmap
            android:src="@drawable/back">
        </bitmap>
    </item>
    <item android:state_pressed="true">
        <bitmap
            android:src="@drawable/back"
            android:tint="@color/colorAccent">
        </bitmap>
    </item>
</selector>

I have tried using app:srcCompat instead of and in addition to android:src, but I still get the error.

R.drawable.back is made up of 5 files of different dpi's: anydpi (vector), mdpi, hdpi, xhdpi, and xxhdpi.

And my gradle settings:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    buildToolsVersion '28.0.3'
    defaultConfig {
        applicationId "com.example.test"
        minSdkVersion 17
        targetSdkVersion 28
        versionCode 5
        versionName "1.1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        vectorDrawables.useSupportLibrary = true
        renderscriptTargetApi 24
        renderscriptSupportModeEnabled true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            zipAlignEnabled true
        }
    }
    lintOptions {
        checkReleaseBuilds false
        abortOnError false
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    implementation('com.android.support:appcompat-v7:28.0.0') { force = true }
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation('com.android.support:design:28.0.0') { force = true }
    implementation('com.android.support:support-v4:28.0.0') { force = true }
    implementation('com.android.support:support-vector-drawable:28.0.0') { force = true }
    testImplementation 'junit:junit:4.12'
    implementation("com.android.support:support-compat:28.0.0") { force = true }
    implementation 'com.squareup.picasso:picasso:2.71828'
    implementation 'commons-io:commons-io:2.5'
    implementation 'com.android.support:exifinterface:28.0.0'
    implementation 'com.android.support:cardview-v7:28.0.0'
}

Also interesting: when I change the drawable I set in the onCreate to R.drawable.back (directly using the drawable instead of using R.drawable.back_button, which is a resource file), there is no error. So crash seems to be due to using vector drawables in a drawable resource file.

UPDATE:

Another error I am getting:

In R.drawable.back_button, when I only use android:src, I get an error:

<bitmap
    android:src="@drawable/back" <!-- ERROR: When using VectorDrawableCompat, you need to use srcCompat-->
    android:tint="@color/colorAccent"/>

But when I use srcCompat

<bitmap <!-- ERROR: required attribute src should be defined-->
    app:srcCompat="@drawable/back"
    android:tint="@color/colorAccent"/>

Using both src and srcCompat doesn't work either.


回答1:


There is no problem with your code, we need to add the @drawable/back in every drawable folder example "main/res/drawable-xxhdpi", "main/res/drawable-xhdpi” …etc then only it will work. (i.e every size ex: hdpi, mdpi,xxhdpi..etc)

we can add the back.png in every folder using android studio by following steps

  1. click “File” and then Click on "New", then "Image Asset"

  1. Change "Asset Type" to "Action Bar and Tab Icons”

  1. For "Foreground" choose “ClipArt"

  1. For "Clipart" click and "Choose" button and pick any icon

  1. For "Resource Name" type in your icon file name

6. Click Next then all sizes of the images(hdpi, mdpi,xxhdpi..etc) will be generated




回答2:


Drawable icon = AppCompatResources.getDrawable(context, R.drawable. back_button);
actionBar.setHomeAsUpIndicator(icon); 

and set below to on create

AppCompatDelegate.setCompatVectorFromResourcesEnabled(true); 


来源:https://stackoverflow.com/questions/54626240/resourcesnotfoundexception-drawable-com-example-testdrawable-back-button-with

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!