Android:getWritableDatabase()上的NullPointerException

时间:2011-09-13 08:22:06

标签: android sqlite nullpointerexception android-2.2-froyo android-sdk-2.1

这是我尝试访问数据库的地方:

UserDataAdapter dbHelper = new UserDataAdapter(this);
dbHelper.open();
dbHelper.createNetwork(network);
dbHelper.close();

UserDataAdapter.java:

public UserDataAdapter open() throws SQLException {
        dbHelper = new DatabaseHelper(context); // of class DatabaseHelper
        database = dbHelper.getWritableDatabase(); // this line!!   
        return this;
    }

DatabaseHelper.java:

public DatabaseHelper(Context context) {
        super(context, Defines.DATABASE_NAME, null, Defines.DATABASE_VERSION);
    }

@Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(Defines.DATABASE_CREATE_TAXI_SERVICE_DATABASE);
    }

崩溃: 09-13 18:12:33.119:E

RROR/AndroidRuntime(7488): FATAL EXCEPTION: main
09-13 18:12:33.119: ERROR/AndroidRuntime(7488): java.lang.NullPointerException
09-13 18:12:33.119: ERROR/AndroidRuntime(7488):     at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)
09-13 18:12:33.119: ERROR/AndroidRuntime(7488):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:98)
09-13 18:12:33.119: ERROR/AndroidRuntime(7488):     at com.basind.testapp.database.UserDataAdapter.open(UserDataAdapter.java:29)
09-13 18:12:33.119: ERROR/AndroidRuntime(7488):     at com.basind.testapp.FetchNetworks.addToDatabase(FetchNetworks.java:107)
09-13 18:12:33.119: ERROR/AndroidRuntime(7488):     at com.basind.testapp.FetchNetworks.access$1(FetchNetworks.java:105)
09-13 18:12:33.119: ERROR/AndroidRuntime(7488):     at com.basind.testapp.FetchNetworks$1.onResponseReceived(FetchNetworks.java:75)
09-13 18:12:33.119: ERROR/AndroidRuntime(7488):     at com.basind.testapp.async.CallbackWrapper.run(CallbackWrapper.java:15)
09-13 18:12:33.119: ERROR/AndroidRuntime(7488):     at android.os.Handler.handleCallback(Handler.java:587)
09-13 18:12:33.119: ERROR/AndroidRuntime(7488):     at android.os.Handler.dispatchMessage(Handler.java:92)
09-13 18:12:33.119: ERROR/AndroidRuntime(7488):     at android.os.Looper.loop(Looper.java:145)
09-13 18:12:33.119: ERROR/AndroidRuntime(7488):     at android.app.ActivityThread.main(ActivityThread.java:4937)
09-13 18:12:33.119: ERROR/AndroidRuntime(7488):     at java.lang.reflect.Method.invokeNative(Native Method)
09-13 18:12:33.119: ERROR/AndroidRuntime(7488):     at java.lang.reflect.Method.invoke(Method.java:521)
09-13 18:12:33.119: ERROR/AndroidRuntime(7488):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-13 18:12:33.119: ERROR/AndroidRuntime(7488):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-13 18:12:33.119: ERROR/AndroidRuntime(7488):     at dalvik.system.NativeStart.main(Native Method)

其他细节:

  • 目标:android-7
  • 测试设备:HTC Desire

3 个答案:

答案 0 :(得分:4)

确保您Context的设置是否正确。

private Context context;
private SQLiteDatabase database;
private DatabaseHelper dbHelper;

public UserDataAdapter(Context context) {
    this.context = context;
}

public UserDataAdapter open() throws SQLException
{
    dbHelper = new DatabaseHelper(context);
    database = dbHelper.getWritableDatabase();
    return this;
}

答案 1 :(得分:1)

仅供参考:我在标准的ContentProvider实现中遇到了NullPointerException错误。我终于解决了它,密钥与清单文件中的上下文/意图有关。

最后,我从我的清单活动中忽略了这一行。

<data android:mimeType="vnd.android.cursor.dir/vnd.pmp.smsmessage" />

查看NotePad示例,了解我所说的内容。另外我更喜欢使用这里的http://www.vogella.com/articles/AndroidSQLite/article.html结构,但是因为他的tut是为3.0而设计的,不得不省去2.2实现的所有加载器内容......

将其放入我创建的数据库后,我可以插入行。我只是使用onCreate测试了我的默认活动。

ContentValues values = new ContentValues();
values.put("somefieldname","somevalue");
values.put("otherfieldname","othervalue");
getContentResolver().insert(MyProvider.CONTENT_URI, values);

答案 2 :(得分:0)

当您尝试从Service或Activity构造函数打开数据库时,会发生这种情况。尝试将open打开到onCreate()。