求助安卓光标问题!

发布于 2024-11-24 04:15:09 字数 2683 浏览 0 评论 0原文

我尝试使用方法 exit 来测试数据库中的记录是否存在。

public boolean Exists(/*String _id*/) {
   Cursor cursor = mDb.rawQuery("select * from "+ TABLE_USER, null );
   boolean exists = (cursor.getCount() > 0);
   cursor.close();
   return exists;
}

然后我调用它,

if ( Exists() ) // do sth;

但当我运行时它强制关闭!

这是我的日志猫:

07-13 15:06:55.961: ERROR/AndroidRuntime(1407): FATAL EXCEPTION: main
07-13 15:06:55.961: ERROR/AndroidRuntime(1407): java.lang.RuntimeException: Unable to start activity ComponentInfo{project.bm/project.bm.GPSTracerActivity}: java.lang.NullPointerException
07-13 15:06:55.961: ERROR/AndroidRuntime(1407):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1622)
07-13 15:06:55.961: ERROR/AndroidRuntime(1407):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1638)
07-13 15:06:55.961: ERROR/AndroidRuntime(1407):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
07-13 15:06:55.961: ERROR/AndroidRuntime(1407):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:928)
07-13 15:06:55.961: ERROR/AndroidRuntime(1407):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-13 15:06:55.961: ERROR/AndroidRuntime(1407):     at android.os.Looper.loop(Looper.java:123)
07-13 15:06:55.961: ERROR/AndroidRuntime(1407):     at android.app.ActivityThread.main(ActivityThread.java:3647)
07-13 15:06:55.961: ERROR/AndroidRuntime(1407):     at java.lang.reflect.Method.invokeNative(Native Method)
07-13 15:06:55.961: ERROR/AndroidRuntime(1407):     at java.lang.reflect.Method.invoke(Method.java:507)
07-13 15:06:55.961: ERROR/AndroidRuntime(1407):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-13 15:06:55.961: ERROR/AndroidRuntime(1407):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-13 15:06:55.961: ERROR/AndroidRuntime(1407):     at dalvik.system.NativeStart.main(Native Method)
07-13 15:06:55.961: ERROR/AndroidRuntime(1407): Caused by: java.lang.NullPointerException
07-13 15:06:55.961: ERROR/AndroidRuntime(1407):     at project.bm.GPSTracerActivity.Exists(GPSTracerActivity.java:145)
07-13 15:06:55.961: ERROR/AndroidRuntime(1407):     at project.bm.GPSTracerActivity.onCreate(GPSTracerActivity.java:47)
07-13 15:06:55.961: ERROR/AndroidRuntime(1407):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-13 15:06:55.961: ERROR/AndroidRuntime(1407):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1586)
07-13 15:06:55.961: ERROR/AndroidRuntime(1407):     ... 11 more

I try to test whether records in database exists by using methods exist .

public boolean Exists(/*String _id*/) {
   Cursor cursor = mDb.rawQuery("select * from "+ TABLE_USER, null );
   boolean exists = (cursor.getCount() > 0);
   cursor.close();
   return exists;
}

then i call it in

if ( Exists() ) // do sth;

But it force to close when i run !

here my log cat :

07-13 15:06:55.961: ERROR/AndroidRuntime(1407): FATAL EXCEPTION: main
07-13 15:06:55.961: ERROR/AndroidRuntime(1407): java.lang.RuntimeException: Unable to start activity ComponentInfo{project.bm/project.bm.GPSTracerActivity}: java.lang.NullPointerException
07-13 15:06:55.961: ERROR/AndroidRuntime(1407):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1622)
07-13 15:06:55.961: ERROR/AndroidRuntime(1407):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1638)
07-13 15:06:55.961: ERROR/AndroidRuntime(1407):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
07-13 15:06:55.961: ERROR/AndroidRuntime(1407):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:928)
07-13 15:06:55.961: ERROR/AndroidRuntime(1407):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-13 15:06:55.961: ERROR/AndroidRuntime(1407):     at android.os.Looper.loop(Looper.java:123)
07-13 15:06:55.961: ERROR/AndroidRuntime(1407):     at android.app.ActivityThread.main(ActivityThread.java:3647)
07-13 15:06:55.961: ERROR/AndroidRuntime(1407):     at java.lang.reflect.Method.invokeNative(Native Method)
07-13 15:06:55.961: ERROR/AndroidRuntime(1407):     at java.lang.reflect.Method.invoke(Method.java:507)
07-13 15:06:55.961: ERROR/AndroidRuntime(1407):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-13 15:06:55.961: ERROR/AndroidRuntime(1407):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-13 15:06:55.961: ERROR/AndroidRuntime(1407):     at dalvik.system.NativeStart.main(Native Method)
07-13 15:06:55.961: ERROR/AndroidRuntime(1407): Caused by: java.lang.NullPointerException
07-13 15:06:55.961: ERROR/AndroidRuntime(1407):     at project.bm.GPSTracerActivity.Exists(GPSTracerActivity.java:145)
07-13 15:06:55.961: ERROR/AndroidRuntime(1407):     at project.bm.GPSTracerActivity.onCreate(GPSTracerActivity.java:47)
07-13 15:06:55.961: ERROR/AndroidRuntime(1407):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-13 15:06:55.961: ERROR/AndroidRuntime(1407):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1586)
07-13 15:06:55.961: ERROR/AndroidRuntime(1407):     ... 11 more

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

所谓喜欢 2024-12-01 04:15:38

看看你没有用mDb对象打开数据库。首先打开数据库,然后运行查询。还将Cursor相关代码放在try/catch块中。否则你将被强制关闭。

See your didn't open the database with mDb object.First open the database and then run the query.Also put Cursor related code in try/catch block.Otherwise you will get force close.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文