App crashes on using findViewWithTag and getTag

Viewed 192

I am a novice android developer. I have declared a 3x3 gridLayout in my activity layout, in which I have put 9 imageViews with tags from 0 to 8. A snippet of the layout code is posted below:

<GridLayout
        android:id="@+id/gridLayout"
        android:background="@drawable/board"
        android:columnCount="3"
        android:rowCount="3">

        <ImageView
            android:id="@+id/counter_topLeft"
            android:onClick="drop_in"
            android:tag="0"
            android:layout_column="0"
            android:layout_row="0" />

In my main method, in the method drop_in(), I'm trying to check if user has called it or not, and if the system has called it, I'm trying to retrieve the ImageView by tags, by using a random number as tag. Here is the drop_in() method:

public void drop_in(View view) // method invoked on tapping any grid cell
    {
        int i;
        ImageView counter = (ImageView) view;
        i = Integer.parseInt(counter.getTag().toString()); // getting the associated tags or basically cell number
        if(isHuman)
        { // do some stuff and then 
            isHuman=false;
        }
        //  now app will perform actions and wait for user input to do stuff again
        if(!isHuman)
        {
            Random random=new Random();
            i=random.nextInt(9);

            Object o=i;
            ViewGroup group = (ViewGroup) findViewById(R.id.gridLayout)
            myview=(View) group.findViewWithTag(o);
            Log.i("Info","View tag is "+myview.getTag().toString());
            isHuman=true;
         }
    }

In the logcat, it shows the following error:

2020-05-15 12:26:40.605 10000-10000/com.s090.tttsingleplayer E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.s090.tttsingleplayer, PID: 10000
    java.lang.IllegalStateException: Could not execute method for android:onClick
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.view.View.getTag()' on a null object reference
        at com.s090.tttsingleplayer.MainActivity.drop_in(MainActivity.java:133)

How can I retrieve the views using tags?

2 Answers

Use this line alse

ImageView counter = (ImageView) view.findViewById(R.id.counter_topLeft);

myview=(View) group.findViewWithTag(String.valueOf(i));

Here is a quote I have copied from Official Android developer site :

Tags

Unlike IDs, tags are not used to identify views. Tags are essentially an extra piece of information that can be associated with a view. They are most often used as a convenience to store data related to views in the views themselves rather than by putting them in a separate structure.

So you must not use tags to find them in runtime. My suggestion is to use an int array to store your ImageViews' IDs and use Random class to generate random indexes with that :

int[] imageviewIds={R.id.image1,R.id.image2 \* .etc*\};
Random random=new Random();
i=random.nextInt(9);
myview=(View) group.findViewById(imageIds[i]);
Related