显然,在我得到一个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");
}