0

尝试打开 SQLiteDatabase 时发生崩溃,我使用 Null 指针异常进行编写。下面是我实例化 SQLiteOpenHelper 的方法:

private final OpenHelper mOpenHelper;

public static DataManager getInstance() {
    if (sInstance == null) {
        sInstance = new DataManager();
    }
    return sInstance;
}

private DataManager() {
    mOpenHelper = new OpenHelper(MainApp.getInstance(), MainApp.getInstance().getWalletToken());
    SQLiteDatabase.loadLibs(MainApp.getInstance());
}

其中 MainApp 是我的应用程序对象。

这是我正在尝试做的事情:

    SQLiteDatabase db = null;
    try {
        db = mOpenHelper.getWritableDatabase();

        for (Card card : cards) {
            if (card == null) {
                continue;
            }

            addCard(card, db);

            ContentValues values = new ContentValues();
            values.put(DataEntry.COLUMN_NAME_NICKNAME, card.getNickname());

            File imageFile = getImageFile(Integer.toString(card.getIndex()));
            if (ImageUtils.saveImage(card.getCardImage(), imageFile)) {
                values.put(DataEntry.COLUMN_NAME_IMAGE_FILE, imageFile.getAbsolutePath());
            }

            String whereClause = DataEntry.COLUMN_NAME_CARD_INDEX + " = "
                    + card.getIndex();
            db.update(DataEntry.TABLE_NAME, values, whereClause, null);
        }
    } finally {
        if (db != null) {
            db.close();
        }
    }        

这是崩溃输出:

03-10 16:49:10.533: E/AndroidRuntime(8771): FATAL EXCEPTION: main
03-10 16:49:10.533: E/AndroidRuntime(8771): java.lang.NullPointerException
03-10 16:49:10.533: E/AndroidRuntime(8771):     at net.sqlcipher.database.SQLiteDatabase.<init>(SQLiteDatabase.java:1957)
03-10 16:49:10.533: E/AndroidRuntime(8771):     at net.sqlcipher.database.SQLiteDatabase.openDatabase(SQLiteDatabase.java:901)
03-10 16:49:10.533: E/AndroidRuntime(8771):     at net.sqlcipher.database.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:944)
03-10 16:49:10.533: E/AndroidRuntime(8771):     at net.sqlcipher.database.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:107)
03-10 16:49:10.533: E/AndroidRuntime(8771):     at com.mywebsite.DataManager$OpenHelper.getWritableDatabase(DataManager.java:282)

我搜索了源代码,但该特定行不包含任何应该引发异常的内容。我究竟做错了什么?

4

1 回答 1

1

loadLibs()之前会打电话new OpenHelper()

除此之外,我的猜测是getWalletToken()正在返回nullMainApp.getInstance()没有返回null,否则你会在getWalletToken()通话本身崩溃。用户如何提供这个“钱包令牌”?

于 2014-03-10T21:16:25.587 回答