下面是相关代码
@Override
public boolean onContextItemSelected(MenuItem item)throws IndexOutOfBoundsException{
AdapterContextMenuInfo itemInfo = (AdapterContextMenuInfo) item.getMenuInfo(); //获取菜单信息
switch(item.getItemId()){
case 1:
int pos = (int)getListAdapter().getItemId(itemInfo.position);//此行报空指针错
list.remove(pos);
adapter.notifyDataSetChanged();
DBAdapter dbAdapter = new DBAdapter(NoteActivity.this);
dbAdapter.deleteOneData(pos);
checkNull();
Toast.makeText(getApplicationContext(), "已删除此记事", Toast.LENGTH_SHORT).show();
break;
case 2:
Intent intent = new Intent(NoteActivity.this,EditActivity.class);
startActivity(intent);
}
return super.onContextItemSelected(item);
}
public List<Map<String, String>> getContent(){
List<Map<String, String>>list = new ArrayList<Map<String,String>>();
Map<String,String>map = new HashMap<String, String>();
DBAdapter dbAdapter = new DBAdapter(NoteActivity.this);
dbAdapter.open();
Note[] content = dbAdapter.getAllData();
if(content!=null){
for(int i=0;i<content.length;i++){
map = new HashMap<String, String>();
map.put("num", "NO."+String.valueOf(i+1));
map.put("title", content[i].title);
list.add(map);
}
}
if(!list.isEmpty()){
return list;
}else {
return null;
}
}
public void listAdapter(){
adapter= new SimpleAdapter(this,getContent(),R.layout.list,new String[]{"num","title"},new int[]{R.id.num,R.id.title});
listView.setAdapter(adapter);
registerForContextMenu(getListView());//注册,使listview上的选项都作为一个菜单 }
请问这个空指针错误是怎么回事,该如何解决?谢谢大家了!
@Override
public boolean onContextItemSelected(MenuItem item)throws IndexOutOfBoundsException{
AdapterContextMenuInfo itemInfo = (AdapterContextMenuInfo) item.getMenuInfo(); //获取菜单信息
switch(item.getItemId()){
case 1:
int pos = (int)getListAdapter().getItemId(itemInfo.position);//此行报空指针错
list.remove(pos);
adapter.notifyDataSetChanged();
DBAdapter dbAdapter = new DBAdapter(NoteActivity.this);
dbAdapter.deleteOneData(pos);
checkNull();
Toast.makeText(getApplicationContext(), "已删除此记事", Toast.LENGTH_SHORT).show();
break;
case 2:
Intent intent = new Intent(NoteActivity.this,EditActivity.class);
startActivity(intent);
}
return super.onContextItemSelected(item);
}
public List<Map<String, String>> getContent(){
List<Map<String, String>>list = new ArrayList<Map<String,String>>();
Map<String,String>map = new HashMap<String, String>();
DBAdapter dbAdapter = new DBAdapter(NoteActivity.this);
dbAdapter.open();
Note[] content = dbAdapter.getAllData();
if(content!=null){
for(int i=0;i<content.length;i++){
map = new HashMap<String, String>();
map.put("num", "NO."+String.valueOf(i+1));
map.put("title", content[i].title);
list.add(map);
}
}
if(!list.isEmpty()){
return list;
}else {
return null;
}
}
public void listAdapter(){
adapter= new SimpleAdapter(this,getContent(),R.layout.list,new String[]{"num","title"},new int[]{R.id.num,R.id.title});
listView.setAdapter(adapter);
registerForContextMenu(getListView());//注册,使listview上的选项都作为一个菜单 }
请问这个空指针错误是怎么回事,该如何解决?谢谢大家了!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货