Skip to content Skip to sidebar Skip to footer

App Crashes On Using Findviewwithtag And Gettag

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 pos

Solution 1:

Use this line alse

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

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

Solution 2:

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]);

Post a Comment for "App Crashes On Using Findviewwithtag And Gettag"