光标返回错误

时间:2016-05-06 16:20:35

标签: android cursor

我正在尝试将元素添加到数据库,然后搜索特定条目。但我得到一个游标错误。这是我的DB类代码。该数据库仅包含一列。提前谢谢。

E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not execute method for android:onClick
   at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:275)
   at android.view.View.performClick(View.java:3574)
   at android.view.View$PerformClick.run(View.java:14293)
   at android.os.Handler.handleCallback(Handler.java:605)
   at android.os.Handler.dispatchMessage(Handler.java:92)
   at android.os.Looper.loop(Looper.java:137)
   at android.app.ActivityThread.main(ActivityThread.java:4448)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:511)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
   at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:511)
   at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:270)
   at android.view.View.performClick(View.java:3574) 
   at android.view.View$PerformClick.run(View.java:14293) 
   at android.os.Handler.handleCallback(Handler.java:605) 
   at android.os.Handler.dispatchMessage(Handler.java:92) 
   at android.os.Looper.loop(Looper.java:137) 
   at android.app.ActivityThread.main(ActivityThread.java:4448) 
   at java.lang.reflect.Method.invokeNative(Native Method) 
   at java.lang.reflect.Method.invoke(Method.java:511) 
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823) 
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590) 
   at dalvik.system.NativeStart.main(Native Method) 
Caused by: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
   at android.database.CursorWindow.nativeGetString(Native Method)
   at android.database.CursorWindow.getString(CursorWindow.java:442)
   at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
   at com.example.cr7.cyber.DataB.search(DataB.java:51)
   at com.example.cr7.cyber.MainActivity.searchData(MainActivity.java:39)
   at java.lang.reflect.Method.invokeNative(Native Method) 
   at java.lang.reflect.Method.invoke(Method.java:511) 
   at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:270) 
   at android.view.View.performClick(View.java:3574) 
   at android.view.View$PerformClick.run(View.java:14293) 
   at android.os.Handler.handleCallback(Handler.java:605) 
   at android.os.Handler.dispatchMessage(Handler.java:92) 
   at android.os.Looper.loop(Looper.java:137) 
   at android.app.ActivityThread.main(ActivityThread.java:4448) 
   at java.lang.reflect.Method.invokeNative(Native Method) 
   at java.lang.reflect.Method.invoke(Method.java:511) 
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823) 
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590) 
   at dalvik.system.NativeStart.main(Native Method) 

以下代码的logcat是:

Set qs = db.GetView("Export")

1 个答案:

答案 0 :(得分:0)

while完全没有工作你永远不会改变光标的位置所以它总是成为一个堆栈溢出

执行查询后,您应该询问光标是否为空,是否可以移动到第一个位置,如下所示:

    if(c != null && c.moveToFirst()){      
       dbstring = c.getString(c.getColumnIndex(col_name));           
     }

或者在您的情况下,因为您拥有的唯一列是FirstName,您可以这样看:

    if(c != null && c.moveToFirst()){      
       dbstring = c.getString(0);           
    }

请记住,如果您更改数据库中的任何内容,则应升级数据库版本。