在片段的列表视图中使用删除查询时应用程序崩溃
App crash on using delete query in list view in fragment
我正在处理呼叫拦截器项目,现在我在列表视图中显示被拦截的短信。当我想删除任何消息时,它会给我一个删除消息的警告对话框,现在当我单击它的按钮时,它将执行查询……然后在这一点上它崩溃了。
我不知道它有什么问题。
这是我的代码。
public class BlockedSmsList extends Fragment {
ListView list;
private DbAdapterSmsLog mDbAdapter;
private ArrayList<String> numberList = null;
ArrayList<String> msgBody;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.block_backup, container,
false);
numberList = new ArrayList<String>();
msgBody = new ArrayList<String>();
mDbAdapter = new DbAdapterSmsLog(getActivity().getApplicationContext());
mDbAdapter.open();
list = (ListView) rootView.findViewById(R.id.call_block_listview);
displayLits();
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,final int pos,
long arg3) {
AlertDialog.Builder adb=new AlertDialog.Builder(getActivity());
adb.setTitle("Please Confirm");
adb.setMessage("Are you sure to delete?");
adb.setPositiveButton("Delete", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
mDbAdapter.deleteReminder(pos);
mDbAdapter.close();
displayLits();
}
});
adb.setNeutralButton("Cencel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
arg0.cancel();
}
});
adb.setNegativeButton("Delete All", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
mDbAdapter.deleteAllNumber();
mDbAdapter.close();
displayLits();
}
});
adb.show();
}
});
return rootView;
}
public void displayLits() {
Cursor c = mDbAdapter.fetchAllReminders();
numberList.clear();
c.moveToFirst();
if (c.moveToFirst()) {
while (c.isAfterLast() == false) {
String name = c.getString(c
.getColumnIndex(DbAdapterSmsLog.KEY_MODE));
String body = c.getString(c
.getColumnIndex(DbAdapterSmsLog.KEY_TITLE));
numberList.add(name);
msgBody.add(body);
c.moveToNext();
}
}
SmsCustomAdopter adapter=new SmsCustomAdopter(getActivity().getApplicationContext(), numberList,msgBody);
list.setAdapter(adapter);
}
}
我通过@shayan pourvatan 评论解决了我的问题。那是
"i think you get Exception in mDbAdapter.fetchAllReminders(); because you closed your DB before that line. you have closed with mDbAdapter.close();. remove that line, might be solve your problem, if this don't help you post log cat."
我正在处理呼叫拦截器项目,现在我在列表视图中显示被拦截的短信。当我想删除任何消息时,它会给我一个删除消息的警告对话框,现在当我单击它的按钮时,它将执行查询……然后在这一点上它崩溃了。 我不知道它有什么问题。 这是我的代码。
public class BlockedSmsList extends Fragment {
ListView list;
private DbAdapterSmsLog mDbAdapter;
private ArrayList<String> numberList = null;
ArrayList<String> msgBody;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.block_backup, container,
false);
numberList = new ArrayList<String>();
msgBody = new ArrayList<String>();
mDbAdapter = new DbAdapterSmsLog(getActivity().getApplicationContext());
mDbAdapter.open();
list = (ListView) rootView.findViewById(R.id.call_block_listview);
displayLits();
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,final int pos,
long arg3) {
AlertDialog.Builder adb=new AlertDialog.Builder(getActivity());
adb.setTitle("Please Confirm");
adb.setMessage("Are you sure to delete?");
adb.setPositiveButton("Delete", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
mDbAdapter.deleteReminder(pos);
mDbAdapter.close();
displayLits();
}
});
adb.setNeutralButton("Cencel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
arg0.cancel();
}
});
adb.setNegativeButton("Delete All", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
mDbAdapter.deleteAllNumber();
mDbAdapter.close();
displayLits();
}
});
adb.show();
}
});
return rootView;
}
public void displayLits() {
Cursor c = mDbAdapter.fetchAllReminders();
numberList.clear();
c.moveToFirst();
if (c.moveToFirst()) {
while (c.isAfterLast() == false) {
String name = c.getString(c
.getColumnIndex(DbAdapterSmsLog.KEY_MODE));
String body = c.getString(c
.getColumnIndex(DbAdapterSmsLog.KEY_TITLE));
numberList.add(name);
msgBody.add(body);
c.moveToNext();
}
}
SmsCustomAdopter adapter=new SmsCustomAdopter(getActivity().getApplicationContext(), numberList,msgBody);
list.setAdapter(adapter);
}
}
我通过@shayan pourvatan 评论解决了我的问题。那是 "i think you get Exception in mDbAdapter.fetchAllReminders(); because you closed your DB before that line. you have closed with mDbAdapter.close();. remove that line, might be solve your problem, if this don't help you post log cat."