1

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

我制作了一个自定义 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 
4

1 回答 1

1
@Override
public long getItemId(int position) {
    return position // this needs to just return a long, position usually is best choice
}
于 2013-09-09T21:15:53.577 回答