gpt4 book ai didi

android - 在从游标访问数据之前确保正确初始化了游标

转载 作者:行者123 更新时间:2023-11-29 14:18:10 25 4
gpt4 key购买 nike

我正在尝试从我的数据库中获取数据。

这是我的代码:

String[] columns = new String[] {COLUMN_FACEBOOK_ALBUM_COVER, COLUMN_FACEBOOK_ALBUM_IS_ACTIVE};

String whereClause = COLUMN_FACEBOOK_ALBUM_IS_ACTIVE + "= '" + 1 + "'";

Cursor cAlbum = ourDatabase.query(TABLE_FACEBOOK, columns, whereClause,null, null, null, null);

columns = new String[] {COLUMN_FACEBOOK_BIG_IMAGE, COLUMN_FACEBOOK_IMAGE_IS_ACTIVE};

whereClause = COLUMN_FACEBOOK_IMAGE_IS_ACTIVE + "= '" + 1 + "'";

Cursor cImage = ourDatabase.query(TABLE_FACEBOOK_IMAGES, columns, null,null, null, null, null);

String[] list = new String[cAlbum.getCount()+cImage.getCount()];

int p = 0;

if(cAlbum.getCount() < 1 && cImage.getCount() < 1)
{
cAlbum.close();
cImage.close();
return null;
}

Log.i("cImage length", cImage.getCount()+"");
Log.i("cAlbum length", cAlbum.getCount()+"");

Log.i("list length", list.length+"");

int i = cImage.getColumnIndex(COLUMN_FACEBOOK_BIG_IMAGE);
int j = cAlbum.getColumnIndex(COLUMN_FACEBOOK_ALBUM_COVER);

for (cAlbum.moveToFirst(); !cAlbum.isAfterLast(); cAlbum.moveToNext())
{
list[p] = cAlbum.getString(j);
p++;
}

for (cImage.moveToFirst(); !cImage.isAfterLast(); cImage.moveToNext())
{
list[p] = cImage.getString(i);
Log.i("image length", list[p].length()+"");
p++;
}

cAlbum.close();
cImage.close();

return list;

此行代码失败:

list[p] = cImage.getString(i);

错误信息是:

11-27 12:34:59.683: E/CursorWindow(6901): Failed to read row 2, column 0 from a CursorWindow which has 2 rows, 2 columns.
11-27 12:34:59.683: D/AndroidRuntime(6901): Shutting down VM
11-27 12:34:59.683: W/dalvikvm(6901): threadid=1: thread exiting with uncaught exception (group=0x41e9b930)
11-27 12:34:59.693: E/AndroidRuntime(6901): FATAL EXCEPTION: main
11-27 12:34:59.693: E/AndroidRuntime(6901): java.lang.RuntimeException: Unable to create service com.example.imageswidget.WidgetService: java.lang.IllegalStateException: Couldn't read row 2, col 0 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
11-27 12:34:59.693: E/AndroidRuntime(6901): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2667)
11-27 12:34:59.693: E/AndroidRuntime(6901): at android.app.ActivityThread.access$1600(ActivityThread.java:153)
11-27 12:34:59.693: E/AndroidRuntime(6901): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1329)
11-27 12:34:59.693: E/AndroidRuntime(6901): at android.os.Handler.dispatchMessage(Handler.java:99)
11-27 12:34:59.693: E/AndroidRuntime(6901): at android.os.Looper.loop(Looper.java:137)
11-27 12:34:59.693: E/AndroidRuntime(6901): at android.app.ActivityThread.main(ActivityThread.java:5227)
11-27 12:34:59.693: E/AndroidRuntime(6901): at java.lang.reflect.Method.invokeNative(Native Method)
11-27 12:34:59.693: E/AndroidRuntime(6901): at java.lang.reflect.Method.invoke(Method.java:511)
11-27 12:34:59.693: E/AndroidRuntime(6901): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
11-27 12:34:59.693: E/AndroidRuntime(6901): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
11-27 12:34:59.693: E/AndroidRuntime(6901): at dalvik.system.NativeStart.main(Native Method)
11-27 12:34:59.693: E/AndroidRuntime(6901): Caused by: java.lang.IllegalStateException: Couldn't read row 2, col 0 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
11-27 12:34:59.693: E/AndroidRuntime(6901): at android.database.CursorWindow.nativeGetString(Native Method)
11-27 12:34:59.693: E/AndroidRuntime(6901): at android.database.CursorWindow.getString(CursorWindow.java:434)
11-27 12:34:59.693: E/AndroidRuntime(6901): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
11-27 12:34:59.693: E/AndroidRuntime(6901): at com.example.imageswidget.DataBaseMain.getFacebookImagesForWidget(DataBaseMain.java:943)
11-27 12:34:59.693: E/AndroidRuntime(6901): at com.example.imageswidget.WidgetService.onCreate(WidgetService.java:51)
11-27 12:34:59.693: E/AndroidRuntime(6901): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2657)
11-27 12:34:59.693: E/AndroidRuntime(6901): ... 10 more

对于通话记录

Log.i("image length", list[p].length()+"");

在代码中断之前,我接到的电话很少。问题似乎只发生在某些行上,但错误代码没有告诉我问题出在哪里。

感谢您的帮助。

最佳答案

在我的例子中,这是因为我没有将列包含在传递给 CursorLoader 构造函数的投影中。这意味着我试图从返回的游标中不存在的列访问数据。

我希望这对某人有帮助...

关于android - 在从游标访问数据之前确保正确初始化了游标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20240249/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com