将indexOf与可绘制资源列表一起使用时遇到问题。我的索引 i 给我的值为 -1 (它没有找到可绘制资源)。
Booster booster = new Booster();
Booster booster1 = booster.findBooster(R.drawable.booster_empty);
public Booster findBooster(int boosterID) {
int[] boosterIDs = new int [] {
R.drawable.booster_empty,
R.drawable.booster_posts_1,
};
String[] names = new String[] {
"blah",
"blah"
};
String[] descriptions = new String[] {
"blah",
"blah"
};
String[] boosts = new String[] {
"blah",
"blah"
};
int i = Arrays.asList(boosterIDs).indexOf(boosterID);
Booster booster = new Booster(boosterID, names[i], descriptions[i], boosts[i]);
return booster;
}
我知道这可能是我错过的非常简单的事情。请帮忙!
最佳答案
对原始数组调用 Arrays.asList() 将生成一个包含一个元素(即数组)的列表,而不是您预期的数组到列表的转换。尝试将 int[]
更改为 Integer[]
。
关于java - 将 indexOf 与可绘制资源列表一起使用时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27219917/