【问题标题】:SQlite Database: Unable to find contextSQlite 数据库:无法找到上下文
【发布时间】:2012-12-27 08:10:55
【问题描述】:

显然上下文和下一行中存在一些问题,因为我收到了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");
    } 

【问题讨论】:

  • 尝试使用这个或只是 getApplicationContext() 然后检查
  • 也发布你的数据库助手类的代码。
  • public DBHelper(Context context) { super(context, DBName, null, version); currentContext = context; DBPath = "/data/data/" + context.getPackageName() + "/databases"; createDatabase();
  • 抱歉格式化!

标签: android database sqlite android-context


【解决方案1】:

您应该将您的 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();

【讨论】:

  • DBHelper dbHelper = DBHelper.getInstance(getBaseContext()); MyDb = dbHelper.getWritableDatabase();上下文正在运行,但显示无法打开或关闭数据库。
  • E/SQLiteDatabase(1025): android.database.sqlite.SQLiteCantOpenDatabaseException: unable to open database file 12-27 14:48:11.450: E/SQLiteDatabase(1025): at android.database.sqlite.SQLiteDatabase.dbopen(Native Method) 12-27 14:48:11.450: E/SQLiteDatabase(1025): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1013) 12-27 14:48:11.450: E/SQLiteDatabase(1025): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:986) 12-27 14:48:11.450: E/SQLiteDatabase(1025): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:962)
  • @LilYoda 查看我关于打开和关闭数据库的更新答案
  • @LilYoda 使用:SQLiteDatabase MyDb = DBHelper.getInstance(this).getWritableDatabase(); ,并使用“this”而不是“getBaseContext()”或“getApplicationContext()”,DBHElper.getInstance(this) 方法将为您处理 getApplicationContext。
  • @hsigmond- 我已经关闭了数据库,但是打开它有问题!但是我来这里是为了解决这个问题,谢谢。我再调试一下。 :) 感谢您的帮助。
【解决方案2】:

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

    public class MyDB {

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

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

希望这会对您有所帮助。

【讨论】:

    【解决方案3】:

    试试这个

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

    它应该可以工作。

    【讨论】:

      【解决方案4】:

      你正在使用,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() 块。

      【讨论】:

      • OK 将尝试返回。谢谢。 :)
      • 以上部分不起作用。我使用了没有 finally 但仍然无法打开或关闭数据库。附加 logcat 并在问题中编辑我的代码。
      • 您已经接受了答案。那么现在有什么问题。?
      • 哦,不用担心。我会尝试调试更多。我来这里的问题已经解决了。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-11
      • 2014-08-30
      • 2023-02-16
      相关资源
      最近更新 更多