【问题标题】:Android: Layout object with integer in ID cannot be referencedAndroid:无法引用 ID 中具有整数的布局对象
【发布时间】:2011-10-25 13:48:29
【问题描述】:

我正在尝试以编程方式在我的布局中设置一些 ImageButton 的图像。 为此,我将我的 ImageButtons 命名为 ic_1 到 ic_5。

在我的初始化方法中,我想遍历这 5 个 ImageButtons 并根据可能的图标列表设置它们的图像。 基本上,用户可以更改图标的显示顺序,这反过来会更改 ImageButtons 上的图像。

但是,我似乎无法引用这些按钮,因为它们的 ID 中有一个整数。 我用于此的代码是这样的:

for (int i = 1; i < 2; i++) {

                    String butid = "ic_"+i;
                    int resID = getResources().getIdentifier(butid, "id",
                            getPackageName());
                    ImageButton button = (ImageButton) findViewById(resID);

但是,这会返回 NullPointerException,因为 resID 返回 0。当我在 butid 中仅使用“ic_1”时,它也返回 0。 但是,如果我给 ImageButton 一个 ID 作为 ic_one,它确实有效。但如果我要使用纯文本 id,我将无法遍历 ImageButtons。

首先我认为这意味着 ID 没有正确转换为 R.java 文件,但其中存在按钮,它们各自的 ID 如下所示。

    public static final int ic_1=0x7f05000e;
    public static final int ic_2=0x7f05000f;

有谁知道在布局对象的 ID 中是否真的不可能使用 int,如果是这样,是否可以像我想要的那样循环 ImageButtons 而无需在 ID 中使用整数?一个简单的例子将不胜感激。


有关沃伦的更多信息:

布局规范:

@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>

【问题讨论】:

    标签: android xml layout integer


    【解决方案1】:

    这可能不是你想要做的,但如果你只处理 5 个按钮,你总是可以声明一个 Button Id 的静态数组并循环遍历它们。

    类似:

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

    【讨论】:

    • 这似乎是一个更好的方法。
    • 是的,我将此作为我的备用计划,以防我真的无法找到解决问题的方法,但如果能找到合适的解决方法,我会牢记在心以供将来使用。此外,将来按钮的数量可能会达到 30 多个,我并不真的期待一直制作静态数组。但是我可能最终会选择这个答案,因为它看起来确实是更好的方法。
    • 最终选择了您的答案,因为我寻找更好的答案并没有发现任何有用的信息。不过,您的方法就像一种魅力!但我还是觉得奇怪,用我的方法不行。
    【解决方案2】:

    你从 0 开始,这就是问题所在。

    i =1 ;开始循环

    【讨论】:

    • 实际上是我的错误,我在这里发帖之前搞砸了一点。原始循环确实有 1 而不是 0,但这也不起作用。再次检查以确定,但int i = 1; 也不能解决问题。甚至添加了println 以确保字符串实际上是ic_1(它是),但它仍然不起作用。
    【解决方案3】:

    您的循环以i = 0 开头,但您的ic_11 开头。将循环更改为以1 开头,这应该可以解决问题。

    【讨论】:

    • 实际上是我的错误,我在这里发帖之前搞砸了一点。原始循环确实有 1 而不是 0,但这也不起作用。
    • 你能告诉我们你是如何指定应该使用的布局吗?
    • 在开篇文章中添加了更多信息。希望这会让事情变得更清楚。
    猜你喜欢
    • 1970-01-01
    • 2021-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-26
    • 1970-01-01
    • 1970-01-01
    • 2017-01-07
    相关资源
    最近更新 更多