Jack Smartie
Jack Smartie

Reputation: 443

Android: How to use View's setTag, getTag and findViewWithTag methods?

I'm having some trouble with a section of code for my android app. I keep getting a NullPointerException when trying to set the background of an ImageView object.

Here's the code:

public View getView(int position, View view, ViewGroup parent) {
    ImageView imageView;
    if (view == null) {
        imageView = new ImageView(mContext);
    } else {
        imageView = (ImageView) view;
    }
    imageView.setTag(position);
    return imageView;
}

private OnItemClickListener itemClickListener = new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

        ImageView imageView;
        //Variable i, here, is from a for loop.
        imageView = (ImageView)v.findViewWithTag(i);
        //I get a NullPointerException at the next line, "Log.d"
        Log.d("View 1", imageView.toString());
        //If I get rid of the "Log.d" line above, 
        //the NullPointerException occurs on the next line
        imageView.setBackgroundColor(Color.BLUE);
        imageView = (ImageView)v.findViewWithTag(position);

        Log.d("View 2", imageView.toString());
        imageView.setBackgroundColor(Color.BLUE);

    };
}

I suspect the problem with my code is because of what parameter I'm passing the setTag() method and what parameter I'm passing the findViewWithTag() method. If someone could show me an example of how to set tags and find views with tags, I would greatly appreciate it.

Thank you for your time.

Edit: Here's the error log. I'm not sure where to put it, so I will put it here.

05-04 21:47:24.314: ERROR/AndroidRuntime(335): FATAL EXCEPTION: main
05-04 21:47:24.314: ERROR/AndroidRuntime(335): java.lang.NullPointerException
05-04 21:47:24.314: ERROR/AndroidRuntime(335):     at com.jacksmartie.PhotoMem.MainActivity$1.onItemClick(MainActivity.java:79)
05-04 21:47:24.314: ERROR/AndroidRuntime(335):     at android.widget.AdapterView.performItemClick(AdapterView.java:284)
05-04 21:47:24.314: ERROR/AndroidRuntime(335):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:1696)
05-04 21:47:24.314: ERROR/AndroidRuntime(335):     at android.os.Handler.handleCallback(Handler.java:587)
05-04 21:47:24.314: ERROR/AndroidRuntime(335):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-04 21:47:24.314: ERROR/AndroidRuntime(335):     at android.os.Looper.loop(Looper.java:123)
05-04 21:47:24.314: ERROR/AndroidRuntime(335):     at android.app.ActivityThread.main(ActivityThread.java:4627)
05-04 21:47:24.314: ERROR/AndroidRuntime(335):     at java.lang.reflect.Method.invokeNative(Native Method)
05-04 21:47:24.314: ERROR/AndroidRuntime(335):     at java.lang.reflect.Method.invoke(Method.java:521)
05-04 21:47:24.314: ERROR/AndroidRuntime(335):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-04 21:47:24.314: ERROR/AndroidRuntime(335):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-04 21:47:24.314: ERROR/AndroidRuntime(335):     at dalvik.system.NativeStart.main(Native Method)`

Upvotes: 12

Views: 42276

Answers (3)

Tancho
Tancho

Reputation: 1603

The nullpointer happens because the imageView variable is null, since findViewById did not find the view, so you should make a check if it is null.
Android Log fails since it cannot print a null variable, instead of setting the tag, try to set the ID by doing:

imageView.setId(position);

If I understand correctly you do not use the ID in the adapter and since the View is generated it does not have one by default. The Tag is mostly used to put an object that is needed by the View it self as a helper variable, so use it visely :)

If you explicitly need to use the Tag then check if the View is actually there, meaning check that you are not re-writing the tag somewhere along the way...

Upvotes: 3

jtt
jtt

Reputation: 13541

This line is part of your culprit:

com.jacksmartie.PhotoMem.MainActivity$1.onItemClick(MainActivity.java:79)

Put a breakpoint here:

Log.d("View 1", imageView.toString());

And look what your imageview reference object [imageView] is, I'm expecting that it is null because you aren't linking it properly.

If it is null, this means that your link to the reference view is not right. If that's the case, then you need to assign it properly like so:

Button b = findViewById(R.id.Button01);

However; since you're using what seems to be a ListView, the pulling of that is slightly different. This means that the way you're pulling the view is wrong, do some research, should find something to help clear that up!

Upvotes: 8

Brian Dupuis
Brian Dupuis

Reputation: 8176

Well, you don't say where it's crashing. And it's not clear what you're doing.

public View getView(int position, View view, ViewGroup parent) {
    imageView.setTag(position);
    return imageView;

Where is imageView declared? What's it set to?

private OnItemClickListener itemClickListener = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

                        ImageView imageView;
                        //Variable i, here, is from a for loop.
                        imageView = (ImageView)v.findViewWithTag(i);

You're declaring a second local variable called imageView here, but it'll disappear when you exit scope. If you're relying on this for setting your previous imageView variable, you'll have no joy. i is from a for loop, but how does that relate to the position in the getView() method?

Upvotes: 4

Related Questions