【问题标题】:How to make ArrayList by type like (Button) R.id.buttonId? [duplicate]如何按(Button)R.id.buttonId之类的类型制作ArrayList? [复制]
【发布时间】:2018-02-21 08:56:09
【问题描述】:

在布局代码中,制作了按钮 ID。我需要访问所有按钮并创建一个 ArrayList,在 Java 中通过 findViewById() 加载按钮,将按钮放在 ArrayList 中。
但它显示NullPointerException

java.lang.RuntimeException:无法启动活动 组件信息{com.bqlab.themelab/com.bqlab.themelab.activity.SelectActivity}: java.lang.NullPointerException:尝试调用虚拟方法 空对象上的“布尔 java.util.ArrayList.add(java.lang.Object)” 参考

原因:java.lang.NullPointerException:尝试调用虚拟 方法 'boolean java.util.ArrayList.add(java.lang.Object)' 在 null 对象引用

使用这个表单的原因是,我需要对每个按钮都使用setOnClickListener。

问题是这段代码,感谢您阅读。

selectTopButtons.add((Button) findViewById(R.id.select_top_btn01));
selectTopButtons.add((Button) findViewById(R.id.select_top_btn02));
selectTopButtons.add((Button) findViewById(R.id.select_top_btn03));
selectTopButtons.add((Button) findViewById(R.id.select_top_btn04));
selectTopButtons.add((Button) findViewById(R.id.select_top_btn05));
selectTopButtons.add((Button) findViewById(R.id.select_top_btn06));

for (int i = 0; i < selectTopButtons.size(); i++) {
        final int finalI = i;
        selectTopButtons.get(finalI).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectTopButtons.get(finalI).setBackground(getResources().getDrawable(R.color.colorYellow));
                clickedButton = selectTopButtonNames.get(finalI);
                for (int i = 0; i < selectTopButtons.size(); i++) {
                    if (i == finalI)
                        break;
                    else
                        selectTopButtons.get(finalI).setBackground(getResources().getDrawable(R.color.colorWhite));
                }
            }
        });
    }

【问题讨论】:

  • ArrayList 为空...按钮不是问题
  • 所以我通过 add 方法添加了按钮。是不是走错路了?
  • 不,你没有。 Attempt to invoke ... add() ... on a null reference。由于列表不存在,因此从未添加按钮。

标签: android arraylist nullpointerexception


【解决方案1】:

您不需要数组列表。尝试常规循环。

final Resources res = getResources();

for (int i = 1; i <= 6; i++) {
    // Get the numbers of the views
    String id = String.format("%02d", i);
    // Generate the R.id values
    int resId = res.getIdentifier("select_top_btn"+resId, "id", getPackageName());
    // Find the view
    Button b = (Button) findViewById(resId);
    // setup button
    final Integer iCopy = new Integer(i);
    b.setOnClickListener( ... );
}

另外,clickedButton 与方法参数v 相同。您无需访问列表即可获得点击的视图。

如果你需要一个列表,你可以把List&lt;Button&gt; buttons = new ArrayList&lt;&gt;();放在循环之前,buttons.add(b);放在里面。

【讨论】:

  • 其他类需要clickButton。
  • 我不确定您的意思,但您可能想在下次提问时发布minimal reproducible example
  • 谢谢。我明白了。
【解决方案2】:

你必须先创建一个列表的实例:

List<Button> selectTopButtons= new ArrayList<Button>();

【讨论】:

  • 谢谢,我明白了。
  • @Sinho BTW,你也可以使用我的答案列表。
  • @cricket_007 这不是我想要的答案,但感谢你让我知道了新的答案。
猜你喜欢
  • 1970-01-01
  • 2017-02-02
  • 1970-01-01
  • 2011-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多