2011-07-13 95 views
0

我嘗試通過使用方法來測試數據庫中的記錄是否存在。幫助光標android問題!

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 
+0

儘管沒有獲得足夠的信息,可能'mDb'在這一點上是空的? – MByD

+0

標記NPE線。 –

+0

我將其聲明爲全局私有SQLiteBuilder mDb; 和我已經添加mDb =新 – sayvortana

回答

0

見你沒與MDB打開數據庫object.First打開數據庫,然後運行query.Also放在try/catch語句block.Otherwise光標相關的代碼你會接近力量。

+0

thx它解決了 – sayvortana