English 中文(简体)
Anders: Layout Object with integer in ID cannot be referenced
原标题: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 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>
最佳回答

也许不是你试图做的事,但如果你只处理5个县,那么你就能够永远宣布布顿伊德山的一个固定阵列。

类似:

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);
                }
            }
        }
    }
}
问题回答

you are starting with 0, that is the problem.

开始使用<代码>i =1;

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.





相关问题
Very basic WPF layout question

How do I get this listbox to be maxiumum size, i.e. fill the group box its in. I ve tried width="auto" Height="auto", width="stretch" Height="stretch" annoyingly at the moment, it dynamicly sizes to ...

How to align the text to top of TextView?

How to align the text to top of a TextView? Equivalent Android API for Swings setInsets()? that is top of text should start be in (0,0) of TextView <?xml version="1.0" encoding="utf-8"?> <...

Centring a flow in Shoes

Can I center the contents of a flow in Shoes? I know that a paragraph can be centred like: para Centred paragrpah , :align=> center However, this does not work with flows: flow(:align=> ...

Overlaying with CSS

I want to load a website in an iframe, and overlay the website with some hints about the website that is shown. However, i got stuck at the point that the website has a variable passive white space at ...

How do you normally make a program look beautiful? [closed]

How can I make an Application look nice and not like an amateur pulled it together? I mean graphic-wise. Is there some sort of book you can read regarding beautiful program layouts, etc? I put this ...

热门标签