Code:
Button b = new Button(this);
b.setText(m.group());
RelativeLayout.LayoutParams lP = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
int iDELETE_AFTER_TEST_ID;
if(bLast != null){
/*
* Add a new move button
*/
iId = bLast.getId();
lP.addRule(RelativeLayout.RIGHT_OF, iId);
bLast = b;
bLast.setId(iId + 1);
iDELETE_AFTER_TEST_ID = iId + 1;
}else{
/*
* Add the first of the move buttons
*/
bLast = b;
bLast.setId(R.id.base_button_arrangement_id);
lP.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
iDELETE_AFTER_TEST_ID = R.id.base_button_arrangement_id;
}
b.setLayoutParams(lP);
b.setPadding(20, 10, 20, 50);
Log.d(TAG, " going to add = " + b.getId() + " class = " + b.getClass());
rl.addView(b);
mNumberofMoveOptions++;
View v = findViewById(iDELETE_AFTER_TEST_ID);
if(v != null){
if(v.getClass() == Button.class){
Log.d(TAG, " id = " + b.getId() + " class = " + b.getClass());
}else{
Log.e(TAG, "Corruption has occured - not button type: " +
iDELETE_AFTER_TEST_ID + " class: " + v.getClass());
}
}else{
Log.e(TAG, "Corruption has occured - view is null: " + iDELETE_AFTER_TEST_ID);
}
Debug:
going to add = 2130968581 class = class android.widget.Button
Corruption has occured – not button type: 2130968581 class: class android.widget.TextView
Question:
So even though the debug clearly shows the widget getting created as a Button, when I query it directly afterwards it has been converted or changed into a TextView. Am I querying incorrectly, or is the ID getting recycled or is something else happening?
Buttons that I create before an after this particular one work perfectly fine and are created as and remain of the Button widget type.
Many thanks for any help in advance.
Mark Murphy kindly answered on another board.
The reason appears to be that fact that the seed of the ID’s is driven by a tool generated Id.
Using a user defined Id seems to sort out this issue and some other random problems I was having that would not appear initially to be related at all but actually were.