android.content.res.Resources$NotFoundException 尝试获取可绘制图像时

android.content.res.Resources$NotFoundException when trying to get a drawable image

我搜索了网络,特别是这个网站,但我还没有找到问题的答案。

我的目标:我有一个包含一些元素的 ListView,一个是 ImageButton。我想根据存储在本地 SQLite 数据库中的数据动态设置此按钮的图像。

以下是代码片段:

在数据库中保存可绘制图像:

e.setBild(R.drawable.goal_icon);

如您所见,我将其保存为整数。据我所知,这就是要走的路。

使用图像:

我认为更重要的部分是,我如何尝试获取可绘制图像。这是我用来填充 ListView 的适配器的一部分:

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        LayoutInflater mInflater = (LayoutInflater)
                getContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        convertView = mInflater.inflate(R.layout.list_item_actions, null);
    }

    ImageButton icon = (ImageButton) convertView.findViewById(R.id.icon);
    try{
        icon.setImageResource(getItem(position).getBild());
    }catch(Exception e){
        icon.setImageResource(R.drawable.default_icon);

    }

'getBild' 方法获取一个整数(很明显)。

他总是去接球并给我default_icon。 这是堆栈跟踪:

09-12 12:22:19.636  23635-23635/android.ht.sportstatistik W/ResourceType﹕ No package identifier when getting value for resource number 0x00000002
09-12 12:22:19.637  23635-23635/android.ht.sportstatistik W/ImageView﹕ Unable to find resource: 2
android.content.res.Resources$NotFoundException: Resource ID #0x2
        at android.content.res.Resources.getValue(Resources.java:1307)
        at android.content.res.Resources.getDrawable(Resources.java:826)
        at android.content.Context.getDrawable(Context.java:403)
        at android.widget.ImageView.resolveUri(ImageView.java:747)
        at android.widget.ImageView.onMeasure(ImageView.java:874)
        at android.view.View.measure(View.java:17637)
        at android.widget.RelativeLayout.measureChild(RelativeLayout.java:698)
        at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:482)
        at android.view.View.measure(View.java:17637)
        at android.widget.ListView.measureScrapChild(ListView.java:1207)
        at android.widget.ListView.measureHeightOfChildren(ListView.java:1272)
        at android.widget.ListView.onMeasure(ListView.java:1182)
        at android.view.View.measure(View.java:17637)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
        at android.view.View.measure(View.java:17637)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
        at android.view.View.measure(View.java:17637)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
        at android.view.View.measure(View.java:17637)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
        at android.view.View.measure(View.java:17637)
        at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:851)
        at android.view.View.measure(View.java:17637)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
        at android.view.View.measure(View.java:17637)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
        at android.support.v7.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:453)
        at android.view.View.measure(View.java:17637)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
        at android.view.View.measure(View.java:17637)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
        at android.view.View.measure(View.java:17637)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2618)
        at android.view.View.measure(View.java:17637)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2019)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1177)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1383)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1065)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5901)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
        at android.view.Choreographer.doCallbacks(Choreographer.java:580)
        at android.view.Choreographer.doFrame(Choreographer.java:550)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:211)
        at android.app.ActivityThread.main(ActivityThread.java:5373)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.in

我错过了什么吗?提前致谢!

根据 Android 开发者文档指定 here

setImageresource() 中的整数值必须是 R.java 文件中的资源 ID。

查看您的代码和问题,您似乎在该方法的 id 参数中传递了您自己的值。

我知道您在该方法中设置了正确的值,但不知何故它被存储为其他东西,而您作为图像资源得到的是“2”。 现在当你打开 R.java 文件时,你可能找不到与此值对应的任何条目。

更好的方法是将文件复制到 sdcard 中,然后获取位图,然后使用 setImageBitmap() 方法将其显示在图像视图中。

希望这可以帮助您确定问题。