0

我开发了一个应用程序,允许用户保存他最喜欢的两个停车位。它使用一项服务从 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)");
  }
4

4 回答 4

0

如果您没有 SQLite 浏览器软件,请在此处获取: SQLite Browser

当我使用 SQLite 时,它​​会有所帮助。我之前遇到过这个问题,我通过将id参数添加到查询中来解决它。本质上,您需要将查询更改为:

public Cursor firstentry(){
   String select = "SELECT id, PREF1, PREF2 FROM parking_table WHERE id =1";
   Cursor cursor = db.rawQuery(select, null);
return cursor;

不要忘记调用c.moveToFirst()ifc是光标的名称。

于 2012-04-08T17:52:18.010 回答
0

它的nullpointerexception意思是当时数据库中可能没有任何内容。如果光标为空,您需要处理

单步执行代码实际上是准确查看 null 的唯一方法。尽管很难用您发布的内容来判断,但肯定需要更多代码

于 2012-04-08T17:15:17.993 回答
0

您正在运行 rawsql,因此请检查确切的查询是否对您的数据库有效,SELECT PREF1, PREF2 FROM parking_table WHERE id =1

PREF1 完全是(带大小写)列的名称吗?

于 2012-04-08T17:29:02.413 回答
0

我设法解决了它,一切都很好,我只需要初始化类对象。这是所需的代码行:

this.yourobject = new class(this);

就我而言,它是:

this.dh = new SQLHelper(this);

希望能帮助到你 :)

于 2012-04-11T10:49:32.060 回答