gpt4 book ai didi

Android,在服务中使用 SQLite 命令的方法上出现 NullPointerException

转载 作者:行者123 更新时间:2023-11-30 02:56:11 26 4
gpt4 key购买 nike

只有一个任务,为什么我的 sqlite 命令在方法中,当我从服务中调用它给我写一个 nullpointerexception ? ...当我尝试使用 toast 时,它运行良好,但在我使用 mDbHelper.open/naplnSviatky/close 后,我的应用程序会崩溃。

该方法只有查询命令,在 Activity 和 fragment 中工作正常

这是我的服务

public class SviatokFullFillService extends Service 
{
String username;
private DatabaseOp mDbHelper;

@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}

@Override
public void onStart(Intent intent, int startId)
{
// TODO Auto-generated method stub
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);

username = sharedPrefs.getString("prefUsername", "NULL");

//Toast.makeText(this, username, Toast.LENGTH_SHORT).show();
mDbHelper.open();
mDbHelper.naplnSviatky(username);
mDbHelper.close();
}

}

这是我的 LogCat

04-22 14:16:43.890: E/AndroidRuntime(6560): FATAL EXCEPTION: main
04-22 14:16:43.890: E/AndroidRuntime(6560): java.lang.RuntimeException: Unable to start service sk.tomino.studentDiary.SviatokFullFillService@410f1188 with Intent { cmp=sk.tomino.studentDiary/.SviatokFullFillService }: java.lang.NullPointerException
04-22 14:16:43.890: E/AndroidRuntime(6560): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2950)
04-22 14:16:43.890: E/AndroidRuntime(6560): at android.app.ActivityThread.access$1900(ActivityThread.java:151)
04-22 14:16:43.890: E/AndroidRuntime(6560): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1442)
04-22 14:16:43.890: E/AndroidRuntime(6560): at android.os.Handler.dispatchMessage(Handler.java:99)
04-22 14:16:43.890: E/AndroidRuntime(6560): at android.os.Looper.loop(Looper.java:155)
04-22 14:16:43.890: E/AndroidRuntime(6560): at android.app.ActivityThread.main(ActivityThread.java:5520)
04-22 14:16:43.890: E/AndroidRuntime(6560): at java.lang.reflect.Method.invokeNative(Native Method)
04-22 14:16:43.890: E/AndroidRuntime(6560): at java.lang.reflect.Method.invoke(Method.java:511)
04-22 14:16:43.890: E/AndroidRuntime(6560): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
04-22 14:16:43.890: E/AndroidRuntime(6560): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
04-22 14:16:43.890: E/AndroidRuntime(6560): at dalvik.system.NativeStart.main(Native Method)
04-22 14:16:43.890: E/AndroidRuntime(6560): Caused by: java.lang.NullPointerException
04-22 14:16:43.890: E/AndroidRuntime(6560): at sk.tomino.studentDiary.SviatokFullFillService.onStart(SviatokFullFillService.java:30)
04-22 14:16:43.890: E/AndroidRuntime(6560): at android.app.Service.onStartCommand(Service.java:450)
04-22 14:16:43.890: E/AndroidRuntime(6560): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2916)
04-22 14:16:43.890: E/AndroidRuntime(6560): ... 10 more

最佳答案

您已经在类级别声明了变量 private DatabaseOp mDbHelper; 但是当您在 onStart() 方法中使用它时,您还没有初始化它,因此它是抛出 NullPointerException

关于Android,在服务中使用 SQLite 命令的方法上出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23219415/

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