Android: Layout Object With Integer In Id Cannot Be Referenced
I'm trying to set the images of some ImageButtons in my layout programmatically. To do so, I named my ImageButtons ic_1 to ic_5. During my initialization method, I want to loop th
Solution 1:
It might not be exactly what you were trying to do, but if you are only dealing with 5 buttons, you could always just declare a static array of the the Button Id's and loop through them.
Something like:
privatestaticfinalint[] buttons =
{R.id.ic_1, R.id.ic_2, R.id.ic_3, R.id.ic_4, R.id.ic_5};
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainscreen);
setMenuImage();
initMainScreen();
}
publicvoidinitMainScreen() {
if (standard == false) {
retrieveLinks();
intsize= mso.getHome().getIcon().size();
if (size > 0) {
for (inti=1; i < 2; i++) {
Stringbuttype= mso.getHome().getIcon().get(i)
.getIconName();
System.out.println(buttype.toLowerCase());
inttypeID= getResources().getIdentifier(
buttype.toLowerCase(), "drawable", getPackageName());
//Using the array of button id's directlyImageButtonbutton= (ImageButton) findViewById(buttons[i]);
if(button != null){
button.setImageResource(typeID);
}else{
System.out.println(butid+" "+resID);
}
}
}
}
}
Solution 2:
Solution 3:
You start your loop with i = 0
but your ic_1
starts with 1
. Change the loop to start with 1
, that should do the trick.
Post a Comment for "Android: Layout Object With Integer In Id Cannot Be Referenced"