0

我有带有自定义适配器的 ListView。直到现在我长按上下文菜单。并重新执行此操作以单击此

 getListView().setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() 
    {

        public void onItemClick(AdapterView<?> parent, View v, int position, long id)
        {
            //To register the button with context menu.
                            registerForContextMenu(getListView());
                            openContextMenu(getListView());

        }
    });

只有这个

registerForContextMenu(getListView());

在此更改之前它工作正常,但现在我有 NullPointerExceptioninfo.id并且我不知道如何修复它。感谢您的回复。

 @Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
    Intent i;
    Log.d("item id", String.valueOf(item.getItemId()));
    switch (item.getItemId()) {

    case R.id.spotreba:    
         i = new Intent(this, SpotrebaActivity.class);
         i.putExtra("idAuta", info.id);
         startActivityForResult(i, REQUEST_ADD_AUTO);
         return true;
         .
         .
         .
4

1 回答 1

0

您应该在活动中注册上下文菜单,ListView而不是在您制作的内部类中(onItemClickListener)..

写成..

registerForContextMenu(getListView());
getListView().setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() 
    {

        public void onItemClick(AdapterView<?> parent, View v, int position, long id)
        {
               openContextMenu(getListView());
        }
    });
于 2013-08-31T12:07:22.560 回答