Android getWritableDatabase()抛出NullPointerException

时间:2013-08-12 14:44:05

标签: android service nullpointerexception android-context getwritabledatabase

我遇到了对getWritableDatabase()的调用问题。它返回一个NullPointerException。最奇怪的是,这个错误只是在我在Android清单中注释掉几行代码之后才开始发生的。现在项目又回到了原来的位置(我没有对这些部分进行评论,但它仍然会抛出NullPointerException)。所以我检查了我的上下文并尝试了this.getBaseContext()和this.getApplicationContext()。

使用getApplicationContext()将其发送到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

使用getBaseContext()将其发送到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

这是我的相关NetService代码。它来自我的第一个活动:

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   
    }
}

我在Manifest中注释掉的行只能让我进入默认主屏幕,因此我可以更改一些设置。通常,我希望这个应用程序替换HOME,因此android.intent.category.HOME属性。我怀疑这是问题,但是评论这三行是触发它的原因。

<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> 
        ...

我发布的唯一原因是因为我找到的其他解决方案都没有。我希望我只是错过了一些简单的事情......提前致谢!

编辑:我找到了一个解决方案,它是在我每次调用它之前实例化db(在我的AsyncTask方法中作为参数传递)。这是因为我不能让dbHelper和db同时访问数据库。这是一个不费脑子但现在我有一堆实例化的db变量,我希望只有一个接近顶部。我能做到这一点吗?

1 个答案:

答案 0 :(得分:8)

com.productivity.uptimeapp.NetService.<init>(NetService.java:45)

logcat中的这一行表明实例化您的类(<init>)时出现问题。

在代码中,有以下成员变量初始化:

DbHelper dbHelper = new DbHelper(this.getBaseContext()); 
SQLiteDatabase db = dbHelper.getWritableDatabase();

问题出在那里。在Context之前,您无法将类实例用作onCreate()。将您的dbHelperdb初始化推迟到onCreate()或更晚。