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

时间:2015-09-12 10:47:36

标签: android sqlite android-drawable listadapter android-imagebutton

我特别搜索了网站和本网站,但我还没有找到问题的答案。

我的目标:我有一个带有一些元素的ListView,一个是ImageButton。我想根据存储在本地SQLite数据库中的数据动态设置此Button的Image。

以下是代码段:

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

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。 这是Stacktrace:

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

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

1 个答案:

答案 0 :(得分:0)

根据指定的{/ 3}}

的Android开发人员文档

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

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

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

更好的方法是复制sdcard中的文件,然后获取Bitmap,然后使用setImageBitmap()方法将其显示为imageview。

希望这有助于您确定问题。