这是一些代码,我无法修复自己。
我制作了一个自定义的ArrayAdapter,用包含玩家姓名的TextViews填充我的ListView。
在实例化适配器之前指定整个播放器名称列表时,它可以正常工作。当我单击适配器实例化后添加的列表项时,会出现问题。
我无法弄清楚为什么我会在:
中得到NullPointerExceptionPlayersArrayAdapter.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;
}
}
来自Debug的错误消息:
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
答案 0 :(得分:1)
@Override
public long getItemId(int position) {
return position // this needs to just return a long, position usually is best choice
}