Help cursor android problem !
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): 开发者_JAVA技巧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
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.
精彩评论