the same hierarchy. This view's id is id/0x1.

The code worked on all versions of Android, started to crash when upgrading to SDK 31
Problem in generating views 

         
            for (int i = 0; i < 10; i++) {
                relativeLayouts[i] = new RelativeLayout(context);
                relativeLayouts[i].setId(i);//<-- Pay attention
            }
            for (int i = 0; i < 10; i++) {
                textview[i] = new TextView(context);
                textview[i].setId(i);//<-- Pay attention
            }
            //Here we add views to the activity
            //We get relativeLayouts[1].id == textview[1].id {CRASH}

 

 Android: View.setID(int id) programmatically - how to avoid ID conflicts?

From API level 17 and above, you can call: View.generateViewId()

Then use View.setId(int).

If your app is targeted lower than API level 17, use ViewCompat.generateViewId()

 

Комментариев нет:

Отправить комментарий