gpt4 book ai didi

Android:将新项目添加到列表后,ListView 中的 NullPointerException onItemClick

转载 作者:行者123 更新时间:2023-11-30 03:23:25 24 4
gpt4 key购买 nike

这是一些我自己无法修复的代码。

我制作了一个自定义 ArrayAdapter,用包含玩家姓名的 TextView 填充我的 ListView。

如果在实例化适配器之前指定了整个播放器名称列表,它就会按预期工作。单击适配器实例化后添加的列表项时出现问题。


我不明白为什么我会在以下位置得到 NullPointerException:

PlayersArrayAdapter.getItemId(int position)

在这一行:

return mIdMap.get(item);

当我点击列表中名为“Fries”的第三个玩家时。


带解释的代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_match_settings);

//arraylist to store players names
ArrayList<String> playerNames = new ArrayList<String>();

//players which are on the list from the begining
//when I click on them, "onPlayerListItemClicked" is properly called
//and I get clicked player name in my LogCat
playerNames.add("Pawel");
playerNames.add("Olga");

//listview to display players names
ListView playerListView = (ListView)findViewById(R.id.ms_player_list);

//custom arrayadapter, which gets ArrayList as one of arguements
PlayersArrayAdapter playerListAdapter = new PlayersArrayAdapter(this, R.layout.player_list_item, playerNames);

playerListView.setAdapter(playerListAdapter);
playerListView.setOnItemClickListener(onPlayerListItemClicked);


// THERE IS ROOT OF PROBLEM:
// player "Fries" is also added to the ListView, and is properly displayed
// but when I click on it, I get NullPointerException described above
playerNames.add("Fries");
playerListAdapter.notifyDataSetChanged();
}

OnItemClickListener onPlayerListItemClicked = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, final View view, int position, long id) {
String item = (String)parent.getItemAtPosition(position);
Logger.i("Clicked at item: " + item);
}
};

PlayersArrayAdapter 类:

class PlayersArrayAdapter extends ArrayAdapter<String>{

HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();

public PlayersArrayAdapter(Context context, int textViewResourceId, List<String> objects) {
super(context, textViewResourceId, objects);

for (int i = 0; i < objects.size(); i++)
mIdMap.put(objects.get(i), i);
}

@Override
public long getItemId(int position) {
String item = getItem(position);
return mIdMap.get(item); //at this line NullPointerException throws
}

@Override
public boolean hasStableIds() {
return true;
}

}

来自调试的错误消息:

Thread [<1> main] (Suspended (exception NullPointerException))  
<VM does not provide monitor information>
PlayersArrayAdapter.getItemId(int) line: 119
AbsListView$PerformClick.run() line: 1964
ViewRoot(Handler).handleCallback(Message) line: 587
ViewRoot(Handler).dispatchMessage(Message) line: 92
Looper.loop() line: 130
ActivityThread.main(String[]) line: 3687
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 507
ZygoteInit$MethodAndArgsCaller.run() line: 867
ZygoteInit.main(String[]) line: 625
NativeStart.main(String[]) line: not available [native

最佳答案

@Override
public long getItemId(int position) {
return position // this needs to just return a long, position usually is best choice
}

关于Android:将新项目添加到列表后,ListView 中的 NullPointerException onItemClick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18706854/

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