samedi 12 septembre 2015

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

i searched the web and this site in particular, but i haven't found an answer to my problem yet.

My aim: I have a ListView with some elements, one is an ImageButton. I want to set the Image of this Button dynamically, based on data i have stored in the local SQLite Database.

Here are the code snippets:

Saving the Drawable Image in the database:

e.setBild(R.drawable.goal_icon);

as you can see, i save it as an integer. From what i've read, that's the way to go.

Using the Image:

I think the more important part is, how i try to get the Drawable Image. This is the part of my Adapter i use to fill the 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);

    }

The 'getBild' method gets an integer (obviously).

He allways goes to catch and gives me the default_icon. Here is the 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

Am i missing something? Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire