我已经通过空指针和查询帖子搜索了高低,但仍然无法解决这个问题。我正在使用自定义适配器对 ListView 进行充气,然后使用 ItemLongClickListener 将其注册,从而引发上下文菜单。上下文菜单中填充了从数据库中检索到的数据,这些数据使 ListView 膨胀...同样的行数,显然...但是当我查询第一项(为 int id 传入的位置“0”)时,我得到一个例外。但是,如果我在它传入后将其增加一,我会在最后一行得到相同的异常。所有其他行都很好地显示了查询的数据。我可以包含更完整的代码,但我认为我在这里遗漏了一些更基本的东西......有什么想法吗?提前感谢,我的第一个问题——如果我的帖子需要以不同的方式进行,请也告诉我!
listview.setAdapter(new MyCustomAdapter(text, image));
listview.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View strings,
int position, long id) {
// showToastMessage("" + position);
showDialogOnLongClick(position);
return true;
}
});
对话生成器,它调用数据库函数:
private void showDialogOnLongClick(int position) {
Builder alert = new AlertDialog.Builder(this);
// AlertDialog alert = new AlertDialog.Builder(this).create();
ArrayList mSelectedItems = new ArrayList();
ArrayList<String> listInfo = getListInfo(position);
...
}
并且,数据库的实际查询:
// Getting single list item data
public ArrayList<String> getListInfo(int id) {
DatabaseHelper helper = new DatabaseHelper(this);
database = helper.getReadableDatabase();
//to increment, or not to increment?
id++;
// Cursor cursor =
// database.rawQuery("SELECT * FROM list_data WHERE _id ='"+ id +"'",
// null);
Cursor cursor = database.query("list_data", new String[] { "listTitle",
"listContent", "dateCreated", "dateModified" }, "_id = " + id
+ "", null, null, null, null);
if (cursor != null && (cursor.getCount() > 0))
cursor.moveToFirst();
ArrayList<String> result = new ArrayList<String>();
result.add(cursor.getString(cursor.getColumnIndex("listTitle")));
result.add(cursor.getString(cursor.getColumnIndex("listContent")));
result.add(cursor.getString(cursor.getColumnIndex("dateCreated")));
result.add(cursor.getString(cursor.getColumnIndex("dateModified")));
return result;
}
日志猫:
03-25 15:13:20.113: E/AndroidRuntime(12639): FATAL EXCEPTION: main
03-25 15:13:20.113: E/AndroidRuntime(12639): java.lang.NullPointerException
03-25 15:13:20.113: E/AndroidRuntime(12639): at com.baked.listanywhere.ActivityMain.showDialogOnLongClick(ActivityMain.java:310)
谢谢参观!