Android KitKat及更低版本上的资源$ NotFoundException

时间:2017-02-22 10:49:32

标签: android android-resources

我使用Resources $ NotFoundException让应用程序崩溃,模拟4.4.4及更低版本的应用程序。在更高的Android版本上一切正常。这是代码:

image1.setImageResource(getResources().getIdentifier(test_image,
                                "drawable", getResources().getString(R.string.app_package)));

为什么会发生这种情况以及如何解决这个问题?

错误日志:

android.content.res.Resources$NotFoundException: Resource ID #0x7f020090
                  at android.content.res.Resources.getValue(Resources.java:1123)
                  at android.support.v7.widget.ResourcesWrapper.getValue(ResourcesWrapper.java:204)
                  at android.support.v7.widget.AppCompatDrawableManager.loadDrawableFromDelegates(AppCompatDrawableManager.java:327)
                  at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:192)
                  at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:185)
                  at android.support.v7.content.res.AppCompatResources.getDrawable(AppCompatResources.java:100)
                  at android.support.v7.widget.AppCompatImageHelper.setImageResource(AppCompatImageHelper.java:69)
                  at android.support.v7.widget.AppCompatImageView.setImageResource(AppCompatImageView.java:78)

4 个答案:

答案 0 :(得分:2)

如果是XML:

app:srcCompat用于ImageView,将app:drawableEndCompat用于Textview可绘制。

答案 1 :(得分:0)

请尝试以下操作:Why getDrawable() doesn't work on some Android devices?
尝试在getResources之前插入" ContextCompat"。
像这样:

ContextCompat.getDrawable(Context context, int id)

答案 2 :(得分:0)

可绘制文件夹是否包含具有 $scope.questions = [ { "_id":"5863d3aaacaeddc00663bc07", "user":{ "roles":[ "admin" ], "profileImageURL":"./modules/users/client/img/profile/uploads/ac4fbab396c2f725ed5211524f171136" }, "friend_tag":[ ], "emotion":"Confused", "category":"Religion & Culture", "created":"2016-12-28T15:00:58.777Z" }, { "_id":"5863d3aaacaeddc00663bc07", "user":{ "roles":[ "admin" ], "profileImageURL":"./modules/users/client/img/profile/uploads/ac4fbab396c2f725ed5211524f171136" }, "friend_tag":[ ], "emotion":"Confused", "category":"Moral Ethics", "created":"2016-12-28T15:00:58.777Z" }, { "_id":"5863d3aaacaeddc00663bc07", "user":{ "roles":[ "admin" ], "profileImageURL":"./modules/users/client/img/profile/uploads/ac4fbab396c2f725ed5211524f171136" }, "friend_tag":[ ], "emotion":"Confused", "category":"Environment & Health", "created":"2016-12-28T15:00:58.777Z" }, { "_id":"5863d3aaacaeddc00663bc07", "user":{ "roles":[ "admin" ], "profileImageURL":"./modules/users/client/img/profile/uploads/ac4fbab396c2f725ed5211524f171136" }, "friend_tag":[ ], "emotion":"Confused", "category":"Environment & Health", "created":"2016-12-28T15:00:58.777Z" }, { "_id":"5863d3aaacaeddc00663bc07", "user":{ "roles":[ "admin" ], "profileImageURL":"./modules/users/client/img/profile/uploads/ac4fbab396c2f725ed5211524f171136" }, "friend_tag":[ ], "emotion":"Confused", "category":"Religion & Culture", "created":"2016-12-28T15:00:58.777Z" }, { "_id":"5863d3aaacaeddc00663bc07", "user":{ "roles":[ "admin" ], "profileImageURL":"./modules/users/client/img/profile/uploads/ac4fbab396c2f725ed5211524f171136" }, "friend_tag":[ ], "emotion":"Confused", "category":"Religion & Culture", "created":"2016-12-28T15:00:58.777Z" }, { "_id":"5863d3aaacaeddc00663bc07", "user":{ "roles":[ "admin" ], "profileImageURL":"./modules/users/client/img/profile/uploads/ac4fbab396c2f725ed5211524f171136" }, "friend_tag":[ ], "emotion":"Confused", "category":"Moral Ethics", "created":"2016-12-28T15:00:58.777Z" } ] 值的drawable?

答案 3 :(得分:0)

在将vector drawable用于textview drawableStart时,我遇到了类似的问题。不知道为什么它会因Resources$NotFoundException而崩溃,但我通过创建另一个可绘制的向量并将另一个包裹在这样的层列表中来解决了这个问题

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Note to future self: Need to wrap ic_nav_setting here. Was crashing on 4.4.1 without it-->
    <item android:drawable="@drawable/ic_nav_setting"/>
</layer-list>

我希望这可以帮助以后遇到类似问题的人