Android: How to use View's setTag, getTag and findViewWithTag methods?
<p>I'm having some trouble with a section of code for my android app. I keep getting a <code>NullPointerException</code> when trying to set the background of an <code>ImageView</code> object.</p>

<p><strong>Here's the code:</strong></p>

<pre><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&lt;?&gt; 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);

    };
}
</code></pre>

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

<p>Thank you for your time.</p>

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

<pre><code>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)`
</code></pre>
