gpt4 book ai didi

java - 将项目添加到 ArrayList Java Android 时出错

转载 作者:行者123 更新时间:2023-12-01 10:34:46 26 4
gpt4 key购买 nike

我在尝试向 arrayList 添加项目时遇到了 nullPointerException。下面是执行此操作的函数:

public void addToUtilityCardItems() {
int[] utilitiesLogos = {R.drawable.kenya_power, R.drawable.nairobi_water, R.drawable.startimes, R.drawable.zuku};
String[] utilitiesNames = {"Kenya Power", "Nairobi Water", "Startimes", "Zuku"};
UtilitiesModel utilities = new UtilitiesModel();

for (int i = 0; i < utilitiesNames.length; i++) {
utilities.setUtilityLogo(utilitiesLogos[i]);
utilities.setUtilityName(utilitiesNames[i]);
utilityCardItems.add(utilities);
}

}

我得到的错误是:

java.lang.NullPointerException
at com.sce.kbs.lient.FragmentTwo.addToUtilityCardItems(FragmentTwo.java:82)
at com.sce.kbs.lient.FragmentTwo.onActivityCreated(FragmentTwo.java:56)

请帮忙

最佳答案

如果您想添加多个实用程序对象:

public void addToUtilityCardItems() {
int[] utilitiesLogos = {R.drawable.kenya_power, R.drawable.nairobi_water, R.drawable.startimes, R.drawable.zuku};
String[] utilitiesNames = {"Kenya Power", "Nairobi Water", "Startimes", "Zuku"};


for (int i = 0; i < utilitiesNames.length; i++) {
UtilitiesModel utilities = new UtilitiesModel();
utilities.setUtilityLogo(utilitiesLogos[i]);
utilities.setUtilityName(utilitiesNames[i]);
utilityCardItems.add(utilities);
}

}

关于java - 将项目添加到 ArrayList Java Android 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34830400/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com