微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我只是在使用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文件中,我尝试使用以下方法设置我的矢量drawable:

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上发布了类似的问题,但还没有好的解决方案.如果有人有任何见解,请帮忙!谢谢!

&LT – 编辑 – &GT

在忽略了这个问题并回到它之后,我找到了一个可靠的解决方案来解决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());
  }
}

我也试图将矢量drawable直接放入xml文件中.而且我找不到解决方法,所以我最终只是从我的xml文件中取出它们并使用与上面相同的函数以编程方式在java中注入它们.我在xml中使用矢量drawables得到的错误如下所示,如果有人有更好的解决方案,我很乐意听到它!

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 Vector Draw的支持被禁用.

该问题的评论之一是:

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设置现在已经过时,你只需要在适当的活动中启用它(但是,需要测试).

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐