0

I am creating an imageButton i'm going to setId to the imageButton. But what I got is nullpointerexception error.. can anybody please tell me what have I done wrong? Thank you so much :) I've declared public int count=0;

public int count=0;

final ImageButton button =(ImageButton)convertView.findViewById(R.id.favoritelist_button);
            button.setId(count); //null pointer exception
            count++;

            button.setImageResource(R.drawable.phone);

            button.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    // Do something in response to button click
                    if(!tFave){
                        button.setImageResource(R.drawable.phone);
                        tFave = true;
                    }
                    else{
                        button.setImageResource(R.drawable.gmail);
                        tFave = false;
                    }
                }
            });
user3158109
  • 41
  • 1
  • 4
  • button could be null. post the xml also in which you have image button – Raghunandan Jan 03 '14 at 17:23
  • my guess is convertView.findViewById(R.id.favoritelist_button); does give you null – Tator Jan 03 '14 at 17:23
  • 4
    So, you're trying to reassign an id to an oject that already has one... I think that **R will get really mad** at it!! You see, setId() should be used for those objects you create at runTime, in those occasions where it's really needed – Phantômaxx Jan 03 '14 at 17:24
  • 3
    If you want to store some id in your buttons different, I suggest you to use `setTag()` and `getTag()`. – Renan Bandeira Jan 03 '14 at 17:34

1 Answers1

0

Since you are dealing with convertview I am guessing this code comes from getView of an Adapter for a ListView.

Since views are re-used by the adapter, any changes you make (like changing the view ID) will be carried forward the next time the view is used.

In your case you changed the ID of the button with setID. That probably worked the first time, but the next time through, you can't find the button anymore - the call to findViewById returned null because the ID favoritelist_button has been removed and replaced with the ID you assigned (to count).

Just remove the call to setID. If you need that, post why you think you need it and people will help you find a different solution.

Greg Ennis
  • 14,917
  • 2
  • 69
  • 74
  • my application is to list Korean word that has been favorited. the expandable list will be like: AAAAA- verb- eat (star button) BBBBB- verb- sleep (star button) the star button can be pressed if the user doesn't want to keep the favorite word anymore. so, i have to assign ID to the star button so I know which one has been unfavorited and I can remove that word from database.. – user3158109 Jan 03 '14 at 18:04
  • You can use another way to to do that, for example, someone else suggested using `setTag` and `getTag` to store that information. Do not change the ID. – Greg Ennis Jan 03 '14 at 18:38