gpt4 book ai didi

Android getWritableDatabase() 抛出 NullPointerException

转载 作者:行者123 更新时间:2023-11-29 14:32:17 28 4
gpt4 key购买 nike

我在调用 getWritableDatabase() 时遇到问题。它返回 NullPointerException。最奇怪的是,这个错误是在我注释掉我的 android list 中的几行代码之后才开始发生的。现在项目回到原来的位置(我取消注释这些部分,它仍然抛出 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

这是我的相关网络服务代码。它是从我的第一个 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
}
}

我在我的 list 中注释掉的行只是让我进入默认主屏幕,这样我就可以更改一些设置。通常,我希望这个应用程序替换 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 同时访问数据库。这很简单,但现在我有一堆实例化的数据库变量,我希望只有一个靠近顶部。我可以让它工作吗?

最佳答案

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

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

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

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

问题就在那里。您不能将类实例用作 Context直到 onCreate() .推迟你的dbHelperdb初始化为 onCreate()或以后。

关于Android getWritableDatabase() 抛出 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18189995/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com