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.
第一,我认为这意味着国际发展研究中心的护卫机被适当翻译为R.java案卷,但各县都在场,其遗嘱如下所示。
public static final int ic_1=0x7f05000e;
public static final int ic_2=0x7f05000f;
是否有任何人知道,在排他物的识别中严重不可能使用排他物,如果是的话,如果有可能通过像我想要的图像Buttons,而不需要在国际发展系统中进行分类,则会这样做? 请举一个简单的例子。
更多信息是:
详细情况:
@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 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);
}
}
}
}
}
编外档案中的规格: (所有县)
<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>