java - 来自旧手机 API 18 的可绘制资源的 Android Resources$NotFoundException

标签 java android gradle android-support-library

只有在使用 Android 4.3 和 API 18 的三星 Galaxy Nexus 手机上进行测试时,我才会遇到以下错误。

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)

在我的 Acitivty.java 文件中,我尝试使用以下方法设置我的 vector 可绘制对象:

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

我在我的 build.gradle 文件中使用最新的支持库,如下所示:

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'

我在 StackOverflow 上看到过类似的问题,但还没有很好的解决方案。如果有人有任何见解,请帮助!谢谢!

<--编辑-->

在忽略该问题并返回到它之后,我找到了解决 VectorDrawable 不受支持问题的可靠解决方案。除了建议的内容之外,您还可以使用以下代码创建 VectorDrawables:

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());
  }
}

我在尝试将 vector 可绘制对象直接放入 xml 文件时也遇到了问题。而且我找不到解决这个问题的方法,所以我最终只是将它们从我的 xml 文件中取出并使用与上述相同的功能以编程方式将它们注入(inject) java 中。我在我的 xml 中使用 vector 绘图得到的错误如下所示,如果有人有更好的解决方案,我很乐意听到!

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

最佳答案

你能测试一下下面的代码吗?只是看看是否有效... 然后,我会用更多信息更新答案

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

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

背景

我知道由于 this Google Issue - 205236 ,由于内存问题,对 vector Drawable 的支持被禁用。

该期的评论之一是:

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

然后,

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

因此,他们在
中重新启用了该选项 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.

也许build.gradle 设置现在已经过时,您只需要在适当的 Activity 中启用它(但是,需要测试)。

关于java - 来自旧手机 API 18 的可绘制资源的 Android Resources$NotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38676873/

相关文章:

java - Spring在方法上的@Profile是一个好习惯吗

java - 使用 rsync 将文件复制到目录时,Java Watch 服务从操作系统获取临时文件通知而不是实际文件

java - 操作栏V7 : Why it doesn't work?

android - 以编程方式安装 apk 时出错 : "android.content.ActivityNotFoundException: Unable to find explicit activity class"

android - 如何在 android v2 map 上显示 map 比例尺

java - 查找未正确关闭的数据库连接

android - 在图像上创建 alpha 渐变以创建淡入淡出效果

gradle - 如何仅在gradle中列出主项目的所有任务?

java - 如何将 gradle 多项目存储库作为 git 子模块包含在另一个 gradle 项目中

hadoop - gradle 传递依赖冲突