求助安卓光标问题!
我尝试使用方法 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看看你没有用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.