app尝试连接数据库时输入catch部分

时间:2013-10-12 18:07:09

标签: android sqlite

我不想再提出这个问题了。我是新手。

LogCat是here .. 数据库类here ..还有下面的活动代码..

bDB.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                boolean ok = true;

                try {

                    db.open();
                    String data = etData.getText().toString();
                    db.addThat(data);
                    db.close();

                } catch (Exception e) {
                    ok = false;
                    e.printStackTrace();
                } finally {
                    if (ok) {
                        Dialog d = new Dialog(Main.this);
                        TextView tv = new TextView(Main.this);
                        tv.setText("Conrats! That's done!");
                        d.setTitle("Ok!");
                        d.setContentView(tv);
                        d.show();
                    }
                }
            }

App进入catch部分。等待帮助..

1 个答案:

答案 0 :(得分:2)

在使用之前,您尚未在类Database上初始化ourDatabase。这导致空指针异常。

将方法更改为

    public void open() throws SQLException {
            ourHelper = new DbHelper(ourContext);
            ourDatabase = ourHelper.getWritableDatabase();
    }