问题
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 through these 5 ImageButtons and set their images according to a list of possible icons. Basically users can change the order in which the icons should be shown, which in turn would change the image on the ImageButtons.
However, i can't seem to reference the buttons since they have an Integer in their ID. The code I use for this is as such:
for (int i = 1; i < 2; i++) {
String butid = "ic_"+i;
int resID = getResources().getIdentifier(butid, "id",
getPackageName());
ImageButton button = (ImageButton) findViewById(resID);
However, this returns a NullPointerException as resID returns 0. When I just use "ic_1" in butid, it also returns 0.
However if I give an ImageButton an ID as ic_one, it does work. But if I'm going to use pure-text id's, I won't be able to loop through the ImageButtons.
First I thought this meant the ID's werent properly translated into the R.java file, but the buttons are present in it, with their respective id's as shown below.
public static final int ic_1=0x7f05000e;
public static final int ic_2=0x7f05000f;
Does anyone know if it's seriously not possible to use an int in the ID of a layout object, and if so, if it's possible to loop through the ImageButtons like I want without the need of an integer in the ID? A simple example would be appreciated.
Some more info for Warren:
Layout specification:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainscreen);
setMenuImage();
initMainScreen();
}
Full code that should change the icons:
public void initMainScreen() {
if (standard == false) {
retrieveLinks();
int size = mso.getHome().getIcon().size();
if (size > 0) {
for (int i = 1; i < 2; i++) {
String butid = "ic_"+i;
int resID = getResources().getIdentifier(butid, "id",
getPackageName());
String buttype = mso.getHome().getIcon().get(i)
.getIconName();
System.out.println(buttype.toLowerCase());
int typeID = getResources().getIdentifier(buttype.toLowerCase(),
"drawable", getPackageName());
ImageButton button = (ImageButton) findViewById(resID);
if(button != null){
button.setImageResource(typeID);
}else{
System.out.println(butid+" "+resID);
}
}
}
}
}
Specification in the layout file: (same goes for all buttons)
<ImageButton android:layout_height="wrap_content"
android:layout_weight="33" android:layout_width="wrap_content"
android:src="@drawable/empty_icon" android:onClick="iconClick" android:background="@null" android:id="@+id/ic_1"></ImageButton>
回答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:
private static final int[] buttons =
{R.id.ic_1, R.id.ic_2, R.id.ic_3, R.id.ic_4, R.id.ic_5};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainscreen);
setMenuImage();
initMainScreen();
}
public void initMainScreen() {
if (standard == false) {
retrieveLinks();
int size = mso.getHome().getIcon().size();
if (size > 0) {
for (int i = 1; i < 2; i++) {
String buttype = mso.getHome().getIcon().get(i)
.getIconName();
System.out.println(buttype.toLowerCase());
int typeID = getResources().getIdentifier(
buttype.toLowerCase(), "drawable", getPackageName());
//Using the array of button id's directly
ImageButton button = (ImageButton) findViewById(buttons[i]);
if(button != null){
button.setImageResource(typeID);
}else{
System.out.println(butid+" "+resID);
}
}
}
}
}
回答2:
you are starting with 0, that is the problem.
start the loop from i =1 ;
回答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.
来源:https://stackoverflow.com/questions/7890277/android-layout-object-with-integer-in-id-cannot-be-referenced