2 myc 100 myc_100 于 2013.02.21 09:58 提问

不能打开 SQLite 数据库

我使用下面的代码打开 SQLite 数据库:

dbHelper = new DataBaseHelper(context);
public static void open() throws SQLException {
    try {
        database = dbHelper.getWritableDatabase();
    } catch (Exception e) {
        e.printStackTrace();
        Log.d(Tag.getTag("open()"), "Can't open db");
    }
}

但是获得 NullPointerException 异常。下面是 DataBaseHelper 类

//declarations and stuff
public DataBaseHelper(Context context) {
    super(context, VFS_DATABASE_NAME, null, DATABASE_VERSION);
    this.context = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    Toast.makeText(context, "Creating DataBase for first time...", 2000).show();
    db.execSQL(DATABASE_CREATE);

}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    Log.w(DataBaseHelper.class.getName(),
            "Upgrading database from version " + oldVersion + " to "
                    + newVersion + ", which will destroy all old data");
    db.execSQL("DROP TABLE IF EXISTS" + VFS_DATABASE_TABLE);
    onCreate(db);
}

我找不出哪里出错。这个错误是如何发生的呢?
堆栈跟踪:

02-20 23:16:07.698: W/System.err(4685): java.lang.NullPointerException
02-20 23:16:07.698: W/System.err(4685):     at com.manager.boot.r1223.VSDataSource.open(VSDataSource.java:34)
02-20 23:16:07.698: W/System.err(4685):     at com.manager.boot.r1223.VSDataSource.ScanVirtualSystems(VSDataSource.java:52)
02-20 23:16:07.706: W/System.err(4685):     at com.manager.boot.r1223.OSListActivity.onCreate(OSListActivity.java:49)
02-20 23:16:07.706: W/System.err(4685):     at android.app.Activity.performCreate(Activity.java:4465)
02-20 23:16:07.706: W/System.err(4685):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
02-20 23:16:07.706: W/System.err(4685):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
02-20 23:16:07.706: W/System.err(4685):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
02-20 23:16:07.706: W/System.err(4685):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
02-20 23:16:07.706: W/System.err(4685):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
02-20 23:16:07.706: W/System.err(4685):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-20 23:16:07.706: W/System.err(4685):     at android.os.Looper.loop(Looper.java:137)
02-20 23:16:07.706: W/System.err(4685):     at android.app.ActivityThread.main(ActivityThread.java:4424)
02-20 23:16:07.706: W/System.err(4685):     at java.lang.reflect.Method.invokeNative(Native Method)
02-20 23:16:07.706: W/System.err(4685):     at java.lang.reflect.Method.invoke(Method.java:511)
02-20 23:16:07.706: W/System.err(4685):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-20 23:16:07.706: W/System.err(4685):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-20 23:16:07.706: W/System.err(4685):     at dalvik.system.NativeStart.main(Native Method)

2个回答

hxn_217
hxn_217   2013.02.27 11:02
已采纳

我认为是 static 中的问题,去掉它。
例如:

public class MyActivity extends ListActivity {

   private DatabaseHandler db;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list);

    db = new DatabaseHandler(this);
    ...
}
zlQQhs
zlQQhs   2013.02.25 23:32
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!