4

显然,在我得到一个NullPointerException. 我认为上下文有问题。此外,当我试图从两个活动中打开和关闭数据库时,它会抛出CannotOpenORCloseDatabase错误。请帮助。

DBHelper dbHelper = new DBHelper(this.getApplicationContext());

知道为什么吗?或者,如果有人可以提出解决方法,那就太好了。

public static final String EXT_CATEGORY = "category";

public static final String EXT_URL = "url";

private String tableName = DBHelper.tableName;

private SQLiteDatabase MyDb;

private int category;
Cursor c = null;

public static final String EXT_POS = "position";
private String url;
private int position;
WebView mWebView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    Log.v(LOG_TAG, "onCreate() called");
    super.onCreate(savedInstanceState);

    openAndQueryDatabase();

}

private void openAndQueryDatabase() {

    position = 1;
    category = 0;
    Log.v(LOG_TAG, "onCreate() called 3");
    try {

        DBHelper dbHelper = DBHelper.getInstance(getBaseContext());
        MyDb = dbHelper.getWritableDatabase();


        c = MyDb.rawQuery("SELECT * FROM " + tableName
                + " where Category =" + category + "AND Position ="
                + position, null);
        Log.v(LOG_TAG, "onCreate() called 4");
        // url = c.getString(c.getColumnIndex("Image"));
        url = "www.google.com";
        Log.v(url, " URL in Recipe ");
    } catch (SQLiteException se) {
        Log.e(getClass().getSimpleName(),
                "Could not create or Open the database");
    } 
4

4 回答 4

3

您应该将 DBHelper 声明为静态实例(单例),以确保在任何给定时间仅存在一个 DBHelper。

 public class DBHelper extends SQLiteOpenHelper { 

    private static DBHelper mInstance = null;

    public static DBHelper getInstance(Context activityContext) {

    // Get the application context from the activityContext to prevent leak
    if (mInstance == null) {
      mInstance = new DBHelper (activityContext.getApplicationContext());
    }
    return mInstance;
  }

  /**
   * Private, use the static method "DBHelper.getInstance(Context)" instead.
   */
  private DBHelper (Context applicationContext) {
    super(applicationContext, DATABASE_NAME, null, DATABASE_VERSION);
  }
}

要打开 SQLiteDatabase,请使用:

SQLiteDatabase mDataBase = DBHelper.getInstance(context).getWritableDatabase();

并关闭它

mDataBase.close();
于 2012-12-27T08:53:16.557 回答
2

为此使用构造函数...在调用数据库类时传递上下文作为参数。如下所示。

    public class MyDB {

   private Context context;
    private DatabaseHelper DBHelper;
    private SQLiteDatabase db;

    public MyDB(Context ctx) 
    {
        this.context = ctx;
        DBHelper = new DatabaseHelper(context);
    }
}

希望这会帮助你。

于 2012-12-27T08:34:41.937 回答
2

尝试这个

db = new DbHelper(this.getContext());

它应该工作。

于 2016-12-27T19:00:17.060 回答
1

您正在使用,DBHelper dbHelper = new DBHelper(this.getApplicationContext());

相反,您应该使用,

DBHelper dbHelper = new DBHelper(getApplicationContext());

编辑-

尝试使用

MyDb = getApplicationContext().getWritableDatabase();

代替

DBHelper dbHelper = new DBHelper(this.getApplicationContext());

MyDb = dbHelper.getWritableDatabase();

还尝试按照此答案finally()的建议删除块。

于 2012-12-27T08:21:54.737 回答