2 zxd 72 zxd_72 于 2013.09.10 14:51 提问

ListView 上 NullPointerException onItemClick 异常

下面的代码我不能修复。
我创建了一个自定义的 ArrayAdapter,使用 TextViews 来填充 ListView 列表,包含 player 的名字。
当 player names 的整个列表都在实例 adapter 前指定,就能按预期的运行。
在实例化 adapter后,当我点击一个 list item 添加时,问题就出现了。
当我点击列表中第三个名是"Fries"的 player 时,我不清楚为什么在return mIdMap.get(item);行的PlayersArrayAdapter.getItemId(int position)里面获得一个NullPointerException 异常。
代码:

@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:

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 

1个回答

shaojie519
shaojie519   2013.09.10 15:08

getItemId的调用的方法实际上不就是返回position吗,何必倒过来倒回去

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!