我的应用程序中有一个奇怪的错误。使用后,我将其保留在后台(以用于其他应用程序),然后尝试返回到我的应用程序。获取DBAdapter对象的调用因空指针异常而失败。这是堆栈跟踪

 ERROR/AndroidRuntime(21804): FATAL EXCEPTION: main
 ERROR/AndroidRuntime(21804): java.lang.RuntimeException: Unable to resume activity {com.newproject/com.newproject.ui.MainGalleryUI}: java.lang.NullPointerException
 ERROR/AndroidRuntime(21804):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2124)
 ERROR/AndroidRuntime(21804):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2139)
 ERROR/AndroidRuntime(21804):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1672)
 ERROR/AndroidRuntime(21804):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
 ERROR/AndroidRuntime(21804):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
 ERROR/AndroidRuntime(21804):     at android.os.Handler.dispatchMessage(Handler.java:99)
 ERROR/AndroidRuntime(21804):     at android.os.Looper.loop(Looper.java:130)
 ERROR/AndroidRuntime(21804):     at android.app.ActivityThread.main(ActivityThread.java:3687)
 ERROR/AndroidRuntime(21804):     at java.lang.reflect.Method.invokeNative(Native Method)
 ERROR/AndroidRuntime(21804):     at java.lang.reflect.Method.invoke(Method.java:507)
 ERROR/AndroidRuntime(21804):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
 ERROR/AndroidRuntime(21804):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
 ERROR/AndroidRuntime(21804):     at dalvik.system.NativeStart.main(Native Method)
 ERROR/AndroidRuntime(21804): Caused by: java.lang.NullPointerException
 ERROR/AndroidRuntime(21804):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:118)
 ERROR/AndroidRuntime(21804):     at com.newproject.services.DBAdapter.open(DBAdapter.java:98)
 ERROR/AndroidRuntime(21804):     at com.newproject.services.DBAdapter.getInstance(DBAdapter.java:85)
 ERROR/AndroidRuntime(21804):     at com.newproject.ui.MainUI.onResume(MainUI.java:51)
 ERROR/AndroidRuntime(21804):     at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1150)
 ERROR/AndroidRuntime(21804):     at android.app.Activity.performResume(Activity.java:3832)
 ERROR/AndroidRuntime(21804):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2114)
 ERROR/AndroidRuntime(21804):     ... 12 more
08-13 18:32:02.660: ERROR/(477): Dumpstate > /data/log/dumpstate_app_error


以及一些代码片段:在MainUI.java onResume()上:

  DBAdapter db= DBAdapter.getInstance();


在DBAdapter上:

private DatabaseHelper mDbHelper;
public static DBAdapter instance;
public static DBAdapter getInstance(){
    if (instance==null){
        instance=new DBAdapter();
        instance.open();
    }
    return instance;
}

public DBAdapter(){
}

private synchronized DBAdapter open() throws SQLException {
    mDbHelper = new DatabaseHelper();
    mDb = mDbHelper.getWritableDatabase();
    return this;
}


最难得的是,查看源代码的方法

  public synchronized SQLiteDatabase getWritableDatabase() {
    if (mDatabase != null && mDatabase.isOpen() && !mDatabase.isReadOnly()) {
        return mDatabase;  // The database is already open for business
    }

    if (mIsInitializing) {
        throw new IllegalStateException("getWritableDatabase called recursively");
    }

    // If we have a read-only database open, someone could be using it
    // (though they shouldn't), which would cause a lock to be held on
    // the file, and our attempts to open the database read-write would
    // fail waiting for the file lock.  To prevent that, we acquire the
    // lock on the read-only database, which shuts out other users.

    boolean success = false;
    SQLiteDatabase db = null;
    if (mDatabase != null) mDatabase.lock();
    try {
        mIsInitializing = true;
        if (mName == null) {
            db = SQLiteDatabase.create(null);
        } else {
            db = mContext.openOrCreateDatabase(mName, 0, mFactory);
        }

        int version = db.getVersion();
        if (version != mNewVersion) {
            db.beginTransaction();
            try {
                if (version == 0) {
                    onCreate(db);
                } else {
                    onUpgrade(db, version, mNewVersion);
                }
                db.setVersion(mNewVersion);
                db.setTransactionSuccessful();
            } finally {
                db.endTransaction();
            }
        }

        onOpen(db);
        success = true;
        return db;
    } finally {
        mIsInitializing = false;
        if (success) {
            if (mDatabase != null) {
                try { mDatabase.close(); } catch (Exception e) { }
                mDatabase.unlock();
            }
            mDatabase = db;
        } else {
            if (mDatabase != null) mDatabase.unlock();
            if (db != null) db.close();
        }
    }
}


第118行(至少在我的源代码中与

success = true;


所以对于空指针异常是毫无意义的

提前致谢

编辑:

发现了问题谢谢。就这个

private static class DatabaseHelper extends SQLiteOpenHelper {

    DatabaseHelper() {
        super(Manager.appcontext, DB_NAME, null,DB_VERSION);
    }


我在使用静态变量存储appcontext,但是在被操作系统杀死后,该变量为null

最佳答案

DatabaseHelper中有什么?您需要一个Context来初始化SQLiteOpenHelper,因此很可能是null。

关于java - 打开数据库时扩展空指针异常,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7052011/

10-10 17:16