SQlite数据库:无法找到上下文

时间:2012-12-27 08:10:56

标签: android database sqlite android-context

显然上下文存在一些问题,因为我得到了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 个答案:

答案 0 :(得分: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();

答案 1 :(得分:2)

试试这个

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

它应该有用。

答案 2 :(得分:1)

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

相反,你应该使用,

DBHelper dbHelper = new DBHelper(getApplicationContext());

修改 -

尝试使用

MyDb = getApplicationContext().getWritableDatabase();

而不是

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

MyDb = dbHelper.getWritableDatabase();

同时尝试按照this answer的建议移除finally()块。

答案 3 :(得分:1)

使用构造函数...在调用DB类时传递上下文作为参数。如下。

    public class MyDB {

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

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

希望这会对你有所帮助。