问题
Here is where I try to access the database:
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);
}
Crash: 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)
Other details:
- Target: android-7
- Test Device: HTC Desire
回答1:
Make sure if your Context
is properly being set or not.
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;
}
回答2:
FYI: I've had the NullPointerException error with a standard ContentProvider implementation. I finally solved it and the key is related to context/intent in the manifest file.
In the end I was missing this line from my inside my manifest's activity's .
<data android:mimeType="vnd.android.cursor.dir/vnd.pmp.smsmessage" />
Checkout the NotePad sample for what I am talking about. Also I prefer to use the structure here http://www.vogella.com/articles/AndroidSQLite/article.html however as his tut is designed for 3.0 had to leave out all the loader stuff for 2.2 implementation ...
After putting it in my database created and I could insert rows. I tested this simply using the onCreate for my default activity.
ContentValues values = new ContentValues();
values.put("somefieldname","somevalue");
values.put("otherfieldname","othervalue");
getContentResolver().insert(MyProvider.CONTENT_URI, values);
回答3:
This happens when you try to open the database from a Service or Activity constructor. Try moving the open to onCreate().
来源:https://stackoverflow.com/questions/7398802/android-nullpointerexception-on-getwritabledatabase