SQLite数据库:找不到上下文 [英] SQlite Database: Unable to find context
问题描述
显然有一些问题的背景下,并在下面的行,因为我得到是一个 NullPointerException异常
。我觉得有什么不妥的上下文。此外,当我米试图打开并从两个活动它扔 CannotOpenORCloseDatabase
错误,请帮忙。
DBHelper dbHelper =新DBHelper(this.getApplicationContext());
任何想法,为什么?或者,它会是巨大的,如果有人能提出一个解决方法。
公共静态最后弦乐EXT_CATEGORY =类别;公共静态最后弦乐EXT_URL =URL;私有String tableName值= DBHelper.tableName;私人SQLiteDatabase MYDB;私人诠释类;
光标C = NULL;公共静态最后弦乐EXT_POS =位置;
私人字符串URL;
私人诠释的位置;
的WebView mWebView;@覆盖
保护无效的onCreate(捆绑savedInstanceState){
Log.v(LOG_TAG的onCreate()被称为);
super.onCreate(savedInstanceState); openAndQueryDatabase();}私人无效openAndQueryDatabase(){ 位置= 1;
类= 0;
Log.v(LOG_TAG的onCreate()呼吁3);
尝试{ DBHelper dbHelper = DBHelper.getInstance(getBaseContext());
MYDB = dbHelper.getWritableDatabase();
C = MyDb.rawQuery(SELECT * FROM+ tableName值
+,其中分类=+类别+和位置=
+位置,NULL);
Log.v(LOG_TAG的onCreate()称为4);
// URL = c.getString(c.getColumnIndex(图像));
URL =www.google.com;
Log.v(URL,URL在配方);
}赶上(SQLiteException SE){
Log.e(的getClass()。getSimpleName(),
无法创建或打开数据库);
}
您应声明的DBHelper作为静态实例(单身),以确保只有一个DBHelper在任何给定的时间存在。
公共类DBHelper扩展SQLiteOpenHelper { 私有静态DBHelper mInstance = NULL; 公共静态DBHelper的getInstance(上下文activityContext){ //从activityContext至prevent泄漏的应用程序上下文
如果(mInstance == NULL){
mInstance =新DBHelper(activityContext.getApplicationContext());
}
返回mInstance;
} / **
*私人,使用静态方法DBHelper.getInstance(上下文)代替。
* /
私人DBHelper(上下文的applicationContext){
超(ApplicationContext一DATABASE_NAME,空,DATABASE_VERSION);
}
}
要打开SQLiteDatabase使用:
SQLiteDatabase mDataBase = DBHelper.getInstance(上下文).getWritableDatabase();
和关闭它。
mDataBase.close();
Apparently there is some problem with the context and in the following line as I'm getting a NullPointerException
. I think something is wrong with the context. Also when I"m trying to open and close database from two activities it's throwing CannotOpenORCloseDatabase
Error. Please help.
DBHelper dbHelper = new DBHelper(this.getApplicationContext());
Any idea why? Or it'd be great if someone could suggest a workaround.
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");
}
You should declare your DBHelper as a static instance (singleton) to ensure that only one DBHelper exist at any given time.
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);
}
}
To open the SQLiteDatabase use:
SQLiteDatabase mDataBase = DBHelper.getInstance(context).getWritableDatabase();
and to close it
mDataBase.close();
这篇关于SQLite数据库:找不到上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!