我在带有 SQLite 数据库的 android 中有简单的函数来返回一些记录的计数。
public int getThreadMessagesCount(int thread_id){
int mThreadCount = 0 ;
Cursor cur = db.rawQuery("select count(*) as ThreadCount from tbl_messaging where thread_id = ?", new String[]{thread_id+""});
Log.d("xxx", "query executed");
if (cur.getCount()<=0)
mThreadCount = 0 ;
else
mThreadCount = cur.getInt(cur.getColumnIndex("ThreadCount")) ;
return mThreadCount ;
}
但是当执行函数向我抛出惩罚时。
日志 :
03-02 12:09:03.210: E/AndroidRuntime(32360): FATAL EXCEPTION: main
03-02 12:09:03.210: E/AndroidRuntime(32360): android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
03-02 12:09:03.210: E/AndroidRuntime(32360): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:418)
03-02 12:09:03.210: E/AndroidRuntime(32360): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
03-02 12:09:03.210: E/AndroidRuntime(32360): at android.database.AbstractWindowedCursor.getInt(AbstractWindowedCursor.java:68)
03-02 12:09:03.210: E/AndroidRuntime(32360): at com.example.tawasol1.cls_database.getThreadMessagesCount(cls_database.java:460)
03-02 12:09:03.210: E/AndroidRuntime(32360): at com.example.tawasol1.Cls_act_check_account$5.onClick(Cls_act_check_account.java:377)
03-02 12:09:03.210: E/AndroidRuntime(32360): at android.view.View.performClick(View.java:4261)
03-02 12:09:03.210: E/AndroidRuntime(32360): at android.view.View$PerformClick.run(View.java:17356)
03-02 12:09:03.210: E/AndroidRuntime(32360): at android.os.Handler.handleCallback(Handler.java:615)
03-02 12:09:03.210: E/AndroidRuntime(32360): at android.os.Handler.dispatchMessage(Handler.java:92)
03-02 12:09:03.210: E/AndroidRuntime(32360): at android.os.Looper.loop(Looper.java:137)
03-02 12:09:03.210: E/AndroidRuntime(32360): at android.app.ActivityThread.main(ActivityThread.java:4921)
03-02 12:09:03.210: E/AndroidRuntime(32360): at java.lang.reflect.Method.invokeNative(Native Method)
03-02 12:09:03.210: E/AndroidRuntime(32360): at java.lang.reflect.Method.invoke(Method.java:511)
03-02 12:09:03.210: E/AndroidRuntime(32360): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
03-02 12:09:03.210: E/AndroidRuntime(32360): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
03-02 12:09:03.210: E/AndroidRuntime(32360): at dalvik.system.NativeStart.main(Native Method)