问题
I am having trouble with my call to getWritableDatabase(). It is returning a NullPointerException. The thing that is strangest is that this error only began occurring after I commented out a couple lines of code in my android manifest. Now the project is back to where it was (I un-commented those sections and it still throws a NullPointerException). So I checked my context and tried this.getBaseContext() and this.getApplicationContext().
Using getApplicationContext() puts this out to logcat:
08-12 09:24:20.042: E/AndroidRuntime(5572): FATAL EXCEPTION: main
08-12 09:24:20.042: E/AndroidRuntime(5572): java.lang.RuntimeException: Unable to instantiate service com.productivity.uptimeapp.NetService: java.lang.NullPointerException
08-12 09:24:20.042: E/AndroidRuntime(5572): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2395)
08-12 09:24:20.042: E/AndroidRuntime(5572): at android.app.ActivityThread.access$1600(ActivityThread.java:139)
08-12 09:24:20.042: E/AndroidRuntime(5572): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1313)
08-12 09:24:20.042: E/AndroidRuntime(5572): at android.os.Handler.dispatchMessage(Handler.java:99)
08-12 09:24:20.042: E/AndroidRuntime(5572): at android.os.Looper.loop(Looper.java:137)
08-12 09:24:20.042: E/AndroidRuntime(5572): at android.app.ActivityThread.main(ActivityThread.java:5021)
08-12 09:24:20.042: E/AndroidRuntime(5572): at java.lang.reflect.Method.invokeNative(Native Method)
08-12 09:24:20.042: E/AndroidRuntime(5572): at java.lang.reflect.Method.invoke(Method.java:511)
08-12 09:24:20.042: E/AndroidRuntime(5572): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
08-12 09:24:20.042: E/AndroidRuntime(5572): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
08-12 09:24:20.042: E/AndroidRuntime(5572): at dalvik.system.NativeStart.main(Native Method)
08-12 09:24:20.042: E/AndroidRuntime(5572): Caused by: java.lang.NullPointerException
08-12 09:24:20.042: E/AndroidRuntime(5572): at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:101)
08-12 09:24:20.042: E/AndroidRuntime(5572): at com.productivity.uptimeapp.NetService.<init>(NetService.java:45)
08-12 09:24:20.042: E/AndroidRuntime(5572): at java.lang.Class.newInstanceImpl(Native Method)
08-12 09:24:20.042: E/AndroidRuntime(5572): at java.lang.Class.newInstance(Class.java:1319)
08-12 09:24:20.042: E/AndroidRuntime(5572): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2392)
08-12 09:24:20.042: E/AndroidRuntime(5572): ... 10 more
Using getBaseContext() puts this out to logcat:
08-12 09:28:57.058: E/AndroidRuntime(5833): FATAL EXCEPTION: main
08-12 09:28:57.058: E/AndroidRuntime(5833): java.lang.RuntimeException: Unable to instantiate service com.productivity.uptimeapp.NetService: java.lang.NullPointerException
08-12 09:28:57.058: E/AndroidRuntime(5833): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2395)
08-12 09:28:57.058: E/AndroidRuntime(5833): at android.app.ActivityThread.access$1600(ActivityThread.java:139)
08-12 09:28:57.058: E/AndroidRuntime(5833): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1313)
08-12 09:28:57.058: E/AndroidRuntime(5833): at android.os.Handler.dispatchMessage(Handler.java:99)
08-12 09:28:57.058: E/AndroidRuntime(5833): at android.os.Looper.loop(Looper.java:137)
08-12 09:28:57.058: E/AndroidRuntime(5833): at android.app.ActivityThread.main(ActivityThread.java:5021)
08-12 09:28:57.058: E/AndroidRuntime(5833): at java.lang.reflect.Method.invokeNative(Native Method)
08-12 09:28:57.058: E/AndroidRuntime(5833): at java.lang.reflect.Method.invoke(Method.java:511)
08-12 09:28:57.058: E/AndroidRuntime(5833): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
08-12 09:28:57.058: E/AndroidRuntime(5833): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
08-12 09:28:57.058: E/AndroidRuntime(5833): at dalvik.system.NativeStart.main(Native Method)
08-12 09:28:57.058: E/AndroidRuntime(5833): Caused by: java.lang.NullPointerException
08-12 09:28:57.058: E/AndroidRuntime(5833): at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
08-12 09:28:57.058: E/AndroidRuntime(5833): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
08-12 09:28:57.058: E/AndroidRuntime(5833): at com.productivity.uptimeapp.NetService.<init>(NetService.java:46)
08-12 09:28:57.058: E/AndroidRuntime(5833): at java.lang.Class.newInstanceImpl(Native Method)
08-12 09:28:57.058: E/AndroidRuntime(5833): at java.lang.Class.newInstance(Class.java:1319)
08-12 09:28:57.058: E/AndroidRuntime(5833): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2392)
08-12 09:28:57.058: E/AndroidRuntime(5833): ... 10 more
Here is my relevant NetService Code. It's called from my first Activity:
package com.productivity.uptimeapp;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.Iterator;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
import android.app.Service;
import android.content.Intent;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
public class NetService extends Service{
private static final String TAG = NetService.class.getSimpleName();
//variables for ServerGet
ServerSocket serverSocket;
private static Socket clientSocket;
//server port
private static final int PORT = 8888;
private static InputStreamReader isReader;
private static DataOutputStream dos;
private static PrintWriter printWriter;
private static BufferedReader bReader;
private String mess;
//...end ServerGet variables
ServerGet myFeed;
Client out;
DbHelper dbHelper = new DbHelper(this.getBaseContext());
SQLiteDatabase db = dbHelper.getWritableDatabase();
private final IBinder iBinder = new LocalBinder();
public class LocalBinder extends Binder {
public NetService getService() {
// Return this instance of LocalService so clients can call public methods
return NetService.this;
}
}
@Override
public IBinder onBind(Intent arg0) {
//creates new AsyncTask
myFeed = new ServerGet();
//starts new AsyncTask
myFeed.execute();
return iBinder;
}
//ServerGet class asynchronously gets new strings
private class ServerGet extends AsyncTask <Void, String, Integer>{
//This contains my Server which gets messages from a client and responds accordingly
}
}
The lines that I commented out in my Manifest only served to get me to the default home screen so I could change some settings. Normally, I would want this app to replace HOME thus the android.intent.category.HOME attribute. I am doubtful that this is the problem but commenting out those three lines is what triggered it.
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/fullscreen">
<service android:name="NetService" />
<activity
android:name="com.productivity.uptimeapp.Login"
android:label="@string/app_name"
android:screenOrientation="landscape" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<!-- <category android:name="android.intent.category.DEFAULT" /> -->
<!-- <category android:name="android.intent.category.LAUNCHER" /> -->
<!-- <category android:name="android.intent.category.HOME"/> -->
</intent-filter>
</activity>
...
The only reason I'm posting is because none of the other solutions I have found are working. I hope I'm just missing something simple... Thanks in advance!
EDIT: I have found a solution which was to instantiate db before I called it each time (passed as an argument in my AsyncTask methods). This is because I can not have dbHelper and db accessing the database at the same time. This was a no-brainer but now I have a bunch of instantiated db variables where I'd like to only have one near the top. Can I make that work?
回答1:
com.productivity.uptimeapp.NetService.<init>(NetService.java:45)
This line in the logcat suggests there's a problem instantiating your class (<init>
).
In the code, there's the following member variable initialization:
DbHelper dbHelper = new DbHelper(this.getBaseContext());
SQLiteDatabase db = dbHelper.getWritableDatabase();
The problem is there. You cannot use the class instance as Context
until onCreate()
. Defer your dbHelper
and db
initialization to onCreate()
or later.
来源:https://stackoverflow.com/questions/18189995/android-getwritabledatabase-throws-a-nullpointerexception