我最近除了将png图像(mdpi,hdpi,xhdpi和xxhdpi)添加到我的android studio项目中,将矢量可绘制对象(anydpi)添加到我的android studio项目中。
我立即开始收到此错误:
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
错误导致我的代码出现在这里:
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);
上面的代码在活动的onCreate中调用,在该活动中,我将自定义可绘制对象(R.drawable.back_button)设置为活动的后退按钮。
但是,R.drawable.back_button是一个可绘制资源文件,它在按下时会更改可绘制对象:
<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>
除了android:src之外,我还尝试使用app:srcCompat来代替,但仍然出现错误。
drawable.back由5个不同dpi的文件组成:anydpi(矢量),mdpi,hdpi,xhdpi和xxhdpi。
我的gradle设置:
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'
}
也很有趣:当我将在onCreate中设置的可绘制对象更改为R.drawable.back(直接使用可绘制对象而不是使用资源文件R.drawable.back_button)时,没有错误。因此崩溃似乎是由于在可绘制资源文件中使用了矢量可绘制对象。
更新:
我遇到的另一个错误:
在R.drawable.back_button中,当我仅使用android:src时,出现错误:
<bitmap
android:src="@drawable/back" <!-- ERROR: When using VectorDrawableCompat, you need to use srcCompat-->
android:tint="@color/colorAccent"/>
但是当我使用srcCompat
<bitmap <!-- ERROR: required attribute src should be defined-->
app:srcCompat="@drawable/back"
android:tint="@color/colorAccent"/>
同时使用src和srcCompat都不起作用。
答案 0 :(得分:0)
您的代码没有问题,我们需要在每个drawable文件夹示例中添加@ drawable / back,例如“ main / res / drawable-xxhdpi”,“ main / res / drawable-xhdpi”等 那么它将起作用。 (即每个尺寸,例如:hdpi,mdpi,xxhdpi..etc)
我们可以按照以下步骤使用android studio在每个文件夹中添加back.png
6。单击“下一步”,将生成所有尺寸的图像(hdpi,mdpi,xxhdpi..etc)
答案 1 :(得分:0)
Drawable icon = AppCompatResources.getDrawable(context, R.drawable. back_button);
actionBar.setHomeAsUpIndicator(icon);
,然后在下方设置为“创建”
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);