我尝试使用cursor == null,但没有用,而cursor.getCount == 0,而moveFirst方法..仍然出错,继承我的代码..
public void getColumns()
{
String[] columns = new String[] {"CLASSROOM_ID"};
Cursor c = ourDatabase.query("CLASSROOM_TABLE", columns, "CLASSROOM_AVAILABLE = 0", null,
null, null, null);
if (c.moveToFirst())
{
int[] myid = new int[c.getCount()];
int i = 0;
while (!c.isAfterLast())
{
myid[i]=c.getInt(0);
ContentValues cv = new ContentValues();
cv.put("CLASSROOM_AVAILABLE", 1);
ourDatabase.update("CLASSROOM_TABLE", cv, "CLASSROOM_ID = "+myid[i], null);
i++;
c.moveToNext();
}
}
}
我真的不知道我做错了什么,我该怎么办才能让我的应用程序知道游标找不到行?
这是logcat
10-29 12:04:23.903: D/AndroidRuntime(22782): Shutting down VM
10-29 12:04:23.903: W/dalvikvm(22782): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
10-29 12:04:23.923: E/AndroidRuntime(22782): FATAL EXCEPTION: main
10-29 12:04:23.923: E/AndroidRuntime(22782): java.lang.IllegalStateException: Could not execute method of the activity
10-29 12:04:23.923: E/AndroidRuntime(22782): at android.view.View$1.onClick(View.java:2072)
10-29 12:04:23.923: E/AndroidRuntime(22782): at android.view.View.performClick(View.java:2408)
10-29 12:04:23.923: E/AndroidRuntime(22782): at android.view.View$PerformClick.run(View.java:8816)
10-29 12:04:23.923: E/AndroidRuntime(22782): at android.os.Handler.handleCallback(Handler.java:587)
10-29 12:04:23.923: E/AndroidRuntime(22782): at android.os.Handler.dispatchMessage(Handler.java:92)
10-29 12:04:23.923: E/AndroidRuntime(22782): at android.os.Looper.loop(Looper.java:123)
10-29 12:04:23.923: E/AndroidRuntime(22782): at android.app.ActivityThread.main(ActivityThread.java:4627)
10-29 12:04:23.923: E/AndroidRuntime(22782): at java.lang.reflect.Method.invokeNative(Native Method)
10-29 12:04:23.923: E/AndroidRuntime(22782): at java.lang.reflect.Method.invoke(Method.java:521)
10-29 12:04:23.923: E/AndroidRuntime(22782): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-29 12:04:23.923: E/AndroidRuntime(22782): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-29 12:04:23.923: E/AndroidRuntime(22782): at dalvik.system.NativeStart.main(Native Method)
10-29 12:04:23.923: E/AndroidRuntime(22782): Caused by: java.lang.reflect.InvocationTargetException
10-29 12:04:23.923: E/AndroidRuntime(22782): at my.skul.Mainmenu.sectwenty(Mainmenu.java:71)
10-29 12:04:23.923: E/AndroidRuntime(22782): at java.lang.reflect.Method.invokeNative(Native Method)
10-29 12:04:23.923: E/AndroidRuntime(22782): at java.lang.reflect.Method.invoke(Method.java:521)
10-29 12:04:23.923: E/AndroidRuntime(22782): at android.view.View$1.onClick(View.java:2067)
10-29 12:04:23.923: E/AndroidRuntime(22782): ... 11 more
10-29 12:04:23.923: E/AndroidRuntime(22782): Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
10-29 12:04:23.923: E/AndroidRuntime(22782): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
10-29 12:04:23.923: E/AndroidRuntime(22782): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214)
10-29 12:04:23.923: E/AndroidRuntime(22782): at android.database.AbstractWindowedCursor.getLong(AbstractWindowedCursor.java:99)
10-29 12:04:23.923: E/AndroidRuntime(22782): at my.skul.MyCreateClassrooms.get(MyCreateClassrooms.java:111)
10-29 12:04:23.923: E/AndroidRuntime(22782): ... 15 more
答案 0 :(得分:2)
您需要使用moveToFirst()
将光标移动到第一个位置如果光标为空,此方法将返回false
(或)
另一种方式是:
while (cursor.moveToNext()) {
//Your logic
}