1

我在调用 getWritableDatabase() 时遇到问题。它返回 NullPointerException。最奇怪的是,这个错误是在我在我的 android manifest 中注释掉几行代码之后才开始发生的。现在项目回到了原来的位置(我取消了对这些部分的注释,它仍然抛出 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 同时访问数据库。这很简单,但现在我有一堆实例化的数据库变量,我希望只有一个靠近顶部。我能做到吗?

4

1 回答 1

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

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

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

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

问题就在那里。您不能将类实例用作Contextuntil onCreate()dbHelper将您的和db初始化推迟到onCreate()或以后。

于 2013-08-12T14:56:30.620 回答