【问题标题】:App crash on using delete query in list view in fragment在片段的列表视图中使用删除查询时应用程序崩溃
【发布时间】:2015-04-22 06:06:19
【问题描述】:

我正在处理呼叫拦截器项目,现在我正在列表视图中显示被阻止的短信。当我想删除任何消息时,它会给我警告对话框来删除它,现在当我点击它的按钮时,它会执行查询......然后在这一点上它崩溃了。 我不知道它有什么问题。 这是我的代码。

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);
        }

    }

【问题讨论】:

  • 我认为你在mDbAdapter.fetchAllReminders(); 中得到了异常,因为你在该行之前关闭了你的数据库。您已使用mDbAdapter.close(); 关闭。删除该行,可能会解决您的问题,如果这不能帮助您发布日志猫
  • 我有一个设备连接问题,所以我构建了apk并将其安装在设备上,所以没有日志。
  • @shayanpourvatan..现在可以了。现在我将在哪里关闭我的数据库?
  • 当您不再使用您的数据库时,例如在onPauseonStop,但是如果您将关闭数据库移动到onPause,您必须在onResume 而不是@ 打开它987654328@

标签: android sqlite android-fragments android-listview dialog


【解决方案1】:

我从@shayan pourvatan 评论中解决了我的问题。那是 “我认为您在 mDbAdapter.fetchAllReminders(); 中出现异常;因为您在该行之前关闭了数据库。您已使用 mDbAdapter.close(); 关闭。删除该行,如果这对您没有帮助,可能会解决您的问题发布日志猫。”

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-19
    • 2016-06-20
    • 1970-01-01
    相关资源
    最近更新 更多