本文介绍了Android的SQLite的 - openOrCreateDatabase()NullPointerException异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下DBAdapter(这不是全部,但它的相关的code)。
I have the following DBAdapter (this isn't all of it, but it's the relevant code).
private final String DATABASE_NAME = "seller";
private final String DATABASE_MY_TABLE = "sellertable";
private final Integer DATABASE_VERSION = 1
private final String CREATE_TABLE = "CREATE TABLE seller ........";
public DBAdapter( Context c )
{
this.context = c;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
public DatabaseHelper( Context context )
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate( SQLiteDatabase db )
{
MyLog.d("Creating Tables");
try
{
db.execSQL(CREATE_TABLE);
}
catch (Exception e)
{
e.printStackTrace();
}
}
@Override
public void onUpgrade( SQLiteDatabase db, int oldVersion, int newVersion )
{
MyLog.w("Upgrading database from version " + oldVersion + " to " + newVersion + ". Destroying old data.");
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_MY_TABLE);
onCreate(db);
}
}
// Opens the database
public DBAdapter open() throws SQLException
{
db = DBHelper.getWritableDatabase();
return this;
}
当我尝试 db.open()
我得到了以下内容:
When I try to db.open()
I get the following:
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to instantiate application com.company.app.MyApp: java.lang.NullPointerException
at android.app.LoadedApk.makeApplication(LoadedApk.java:466)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:3260)
at android.app.ActivityThread.access$2200(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:969)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:118)
at com.company.app.DBAdapter.open(DBAdapter.java:74)
at com.company.app.MyApp.<init>(MyApp.java:106)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1409)
at android.app.Instrumentation.newApplication(Instrumentation.java:957)
at android.app.Instrumentation.newApplication(Instrumentation.java:942)
at android.app.LoadedApk.makeApplication(LoadedApk.java:461)
看来它越来越某种异常,与当帮手是决定是否创建或更新数据库...
It appears it's getting some kind of exception, related to when the helper is deciding whether or not to create or update the database...
推荐答案
尝试添加如.db的延伸到你的数据库名称末尾。
Try adding an extension like ".db" to the end of your database name.
这篇关于Android的SQLite的 - openOrCreateDatabase()NullPointerException异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!