When you add two controls with the same ID in a layout, an error occurs through Findviewbyid.
Here's how to fix it:
1. Through the view
generateViewId()
Method gets an available ID. The SetID is manually performed.
Requires API level 17
The code for the function can be used directly below API level 17:
Public Static intGenerateviewid() { for (;;) { Final intresult=Snextgeneratedid.Get(); //aapt-generated IDs has the high byte nonzero, clamp to the range under that. intNewValue=result+ 1; if (NewValue> 0x00ffffff)NewValue= 1; //Roll over to 1, not 0. if (Snextgeneratedid.Compareandset(result,NewValue)) { returnresult; } }}
2. General Method:
Samples/apidemos/src/com/example/android/apis/radiogroup1.java
Samples/apidemp/res/values/ids.xml
Declares an ID item
<resources>
<item type= "id" name= "snack"/>
</resources>
The corresponding name is set in Java code by SetID
Newradiobutton.setid (R.id.snack);
Android Dynamic SetID, avoid duplicate ID