我开发了一个应用程序,允许用户保存他最喜欢的两个停车位。它使用一项服务从 sqlite 数据库中检索相关的停车信息。在数据库中,我有这种方法可以找到第一个条目:
public Cursor firstentry(){
String select = "SELECT PREF1, PREF2 FROM parking_table WHERE id =1";
Cursor cursor = db.rawQuery(select, null);
return cursor;
每次我运行我的应用程序时,都会出现强制关闭错误,当服务尝试使用以下命令调用此方法时:
Cursor c = dh.firstentry();
当我评论它时,它给出了错误,应用程序运行良好。
这是错误的logcat:
04-08 18:56:38.673: E/AndroidRuntime(13166): FATAL EXCEPTION: Timer-0
04-08 18:56:38.673: E/AndroidRuntime(13166): java.lang.NullPointerException
04-08 18:56:38.673: E/AndroidRuntime(13166): at stefan.testservice.ConnectionService$1.run(ConnectionService.java:64)
04-08 18:56:38.673: E/AndroidRuntime(13166): at java.util.Timer$TimerImpl.run(Timer.java:284)
04-08 18:56:39.654: E/ActivityThread(13166): Activity stefan.testservice.TestserviceActivity has leaked IntentReceiver stefan.testservice.TestserviceActivity$2@40521b78 that was originally registered here. Are you missing a call to unregisterReceiver()?
04-08 18:56:39.654: E/ActivityThread(13166): android.app.IntentReceiverLeaked: Activity stefan.testservice.TestserviceActivity has leaked IntentReceiver stefan.testservice.TestserviceActivity$2@40521b78 that was originally registered here. Are you missing a call to unregisterReceiver()?
04-08 18:56:39.654: E/ActivityThread(13166): at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:756)
04-08 18:56:39.654: E/ActivityThread(13166): at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:551)
04-08 18:56:39.654: E/ActivityThread(13166): at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:795)
04-08 18:56:39.654: E/ActivityThread(13166): at android.app.ContextImpl.registerReceiver(ContextImpl.java:782)
04-08 18:56:39.654: E/ActivityThread(13166): at android.app.ContextImpl.registerReceiver(ContextImpl.java:776)
04-08 18:56:39.654: E/ActivityThread(13166): at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:318)
04-08 18:56:39.654: E/ActivityThread(13166): at stefan.testservice.TestserviceActivity$1.onServiceConnected(TestserviceActivity.java:280)
04-08 18:56:39.654: E/ActivityThread(13166): at android.app.LoadedApk$ServiceDispatcher.doConnected(LoadedApk.java:1064)
04-08 18:56:39.654: E/ActivityThread(13166): at android.app.LoadedApk$ServiceDispatcher$RunConnection.run(LoadedApk.java:1081)
04-08 18:56:39.654: E/ActivityThread(13166): at android.os.Handler.handleCallback(Handler.java:587)
04-08 18:56:39.654: E/ActivityThread(13166): at android.os.Handler.dispatchMessage(Handler.java:92)
04-08 18:56:39.654: E/ActivityThread(13166): at android.os.Looper.loop(Looper.java:123)
04-08 18:56:39.654: E/ActivityThread(13166): at android.app.ActivityThread.main(ActivityThread.java:3683)
04-08 18:56:39.654: E/ActivityThread(13166): at java.lang.reflect.Method.invokeNative(Native Method)
04-08 18:56:39.654: E/ActivityThread(13166): at java.lang.reflect.Method.invoke(Method.java:507)
04-08 18:56:39.654: E/ActivityThread(13166): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-08 18:56:39.654: E/ActivityThread(13166): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-08 18:56:39.654: E/ActivityThread(13166): at dalvik.system.NativeStart.main(Native Method)
04-08 18:56:40.054: E/ActivityThread(13166): Activity stefan.testservice.TestserviceActivity has leaked ServiceConnection stefan.testservice.TestserviceActivity$1@40521790 that was originally bound here
04-08 18:56:40.054: E/ActivityThread(13166): android.app.ServiceConnectionLeaked: Activity stefan.testservice.TestserviceActivity has leaked ServiceConnection stefan.testservice.TestserviceActivity$1@40521790 that was originally bound here
04-08 18:56:40.054: E/ActivityThread(13166): at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:938)
04-08 18:56:40.054: E/ActivityThread(13166): at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:833)
04-08 18:56:40.054: E/ActivityThread(13166): at android.app.ContextImpl.bindService(ContextImpl.java:867)
04-08 18:56:40.054: E/ActivityThread(13166): at android.content.ContextWrapper.bindService(ContextWrapper.java:347)
04-08 18:56:40.054: E/ActivityThread(13166): at stefan.testservice.TestserviceActivity.doBindService(TestserviceActivity.java:290)
04-08 18:56:40.054: E/ActivityThread(13166): at stefan.testservice.TestserviceActivity$DownloadTask.onPostExecute(TestserviceActivity.java:193)
04-08 18:56:40.054: E/ActivityThread(13166): at android.os.AsyncTask.finish(AsyncTask.java:417)
04-08 18:56:40.054: E/ActivityThread(13166): at android.os.AsyncTask.access$300(AsyncTask.java:127)
04-08 18:56:40.054: E/ActivityThread(13166): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
04-08 18:56:40.054: E/ActivityThread(13166): at android.os.Handler.dispatchMessage(Handler.java:99)
04-08 18:56:40.054: E/ActivityThread(13166): at android.os.Looper.loop(Looper.java:123)
04-08 18:56:40.054: E/ActivityThread(13166): at android.app.ActivityThread.main(ActivityThread.java:3683)
04-08 18:56:40.054: E/ActivityThread(13166): at java.lang.reflect.Method.invokeNative(Native Method)
04-08 18:56:40.054: E/ActivityThread(13166): at java.lang.reflect.Method.invoke(Method.java:507)
04-08 18:56:40.054: E/ActivityThread(13166): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-08 18:56:40.054: E/ActivityThread(13166): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-08 18:56:40.054: E/ActivityThread(13166): at dalvik.system.NativeStart.main(Native Method)
非常感谢任何帮助,因为我尝试了一切,但似乎没有任何效果。提前致谢!
编辑:
停车位代码:
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_NAME + "(id INTEGER PRIMARY KEY, PREF1 TEXT, PREF2 TEXT)");
}