安卓:在ID的整数布局对象不能被引用 [英] Android: Layout object with integer in ID cannot be referenced

查看:137
本文介绍了安卓:在ID的整数布局对象不能被引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置一些ImageButtons的形象在我的布局以编程方式。
要做到这一点,我叫我的ImageButtons ic_1到ic_5。

在我的初始化方法,我要通过这5 ImageButtons回路和根据可能的图标的列表设定其图像。
基本上,用户可以更改,其中的图标应该显示的顺序,这反过来会改变对ImageButtons图像

不过,我似乎无法引用按钮,因为他们在自己的ID的整数。
在code我用这个是这样的:

 的for(int i = 1;我2;我++){                    字符串butid =_ IC+我;
                    INT渣油= getResources()。则getIdentifier(butid,ID,
                            getPackageName());
                    的ImageButton按钮=(的ImageButton)findViewById(渣油);

不过,这会返回一个NullPointerException异常为渣油回0。当我刚刚使用 butid ic_1它也返回0。
但是,如果我举一个ImageButton的一个ID为ic_one,它的工作。但是如果我要使用纯文本的ID,我将无法通过ImageButtons循环。

首先,我认为,这表示ID的正确的werent翻译成R.java文件,但按键都在上面present,各自的ID,如下图所示。

 公共静态最终诠释ic_1 = 0x7f05000e;
    公共静态最终诠释ic_2 = 0x7f05000f;

有谁知道这是否认真不可能在一个布局对象的ID来使用int,如果是这样,如果有可能遍历ImageButtons像我想,而不需要在ID整数?一个简单的例子是AP preciated。


沃伦一些更多的信息:

布局规范:

  @覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.mainscreen);
    setMenuImage();
    initMainScreen();
}

全部code应该更改图标:

 公共无效initMainScreen(){
    如果(标准== FALSE){
        retrieveLinks();
        INT大小= mso.getHome()调用getIcon()尺寸()。        如果(大小大于0){
            的for(int i = 1;我2;我++){                字符串butid =_ IC+我;
                INT渣油= getResources()。则getIdentifier(butid,ID,
                        getPackageName());                字符串buttype = mso.getHome()调用getIcon()获得(I)
                        .getIconName();
                的System.out.println(buttype.toLowerCase());
                INT TYPEID = getResources()。则getIdentifier(buttype.toLowerCase()
                        可拉伸,getPackageName());                的ImageButton按钮=(的ImageButton)findViewById(渣油);
                如果(按钮!= NULL){
                button.setImageResource(typeid的);
                }其他{
                    的System.out.println(butid ++渣油);
                }
            }
        }
    }
}

规格在布局文件:(同样适用于所有的按钮)

 < ImageButton的机器人:layout_height =WRAP_CONTENT
        机器人:layout_weight =33的android:layout_width =WRAP_CONTENT
        机器人:SRC =@绘制/ empty_icon机器人:的onClick =iconClick机器人:背景=@空机器人:ID =@ + ID / ic_1>< / ImageButton的>


解决方案

这可能不是正是你所试图做的,但如果你只处理5个按钮,你总是可以只申报的的静态数组按钮ID和遍历他们。

是这样的:

 私有静态最终诠释[]按钮=
     {R.id.ic_1,R.id.ic_2,R.id.ic_3,R.id.ic_4,R.id.ic_5};@覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.mainscreen);
    setMenuImage();
    initMainScreen();
}公共无效initMainScreen(){
    如果(标准== FALSE){
        retrieveLinks();
        INT大小= mso.getHome()调用getIcon()尺寸()。        如果(大小大于0){
            的for(int i = 1;我2;我++){                字符串buttype = mso.getHome()调用getIcon()获得(I)
                        .getIconName();
                的System.out.println(buttype.toLowerCase());
                INT TYPEID = getResources()。则getIdentifier(
                        buttype.toLowerCase(),可拉伸,getPackageName());                //直接使用的按钮ID的数组
                的ImageButton按钮=(的ImageButton)findViewById(按钮[I]);
                如果(按钮!= NULL){
                button.setImageResource(typeid的);
                }其他{
                    的System.out.println(butid ++渣油);
                }
            }
        }
    }
}

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>

解决方案

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);
                }
            }
        }
    }
}

这篇关于安卓:在ID的整数布局对象不能被引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆