无法从 CursorWindow SQLite Android 读取第 0 行、第 5 行



我正在从 WidgetRemoteService查询我的 SQLite Database。数据库已正确存储。我无法从数据库中检索url字符串。

这是我对logcat上的

E/UncaughtException: java.lang.IllegalStateException: Couldn't read row 0, col 5 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:438)
                                                                          at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
                                                                          at android.database.CursorWrapper.getString(CursorWrapper.java:137)
                                                                          at io.wyntr.peepster.widget.DetailWidgetRemoteService$1.getViewAt(DetailWidgetRemoteService.java:101)
                                                                          at android.widget.RemoteViewsService$RemoteViewsFactoryAdapter.getViewAt(RemoteViewsService.java:164)
                                                                          at com.android.internal.widget.IRemoteViewsFactory$Stub.onTransact(IRemoteViewsFactory.java:85)
                                                                          at android.os.Binder.execTransact(Binder.java:453)

这是我在widgetintentservice

中的查询
private static final String[] FEEDS_COLUMNS = {
            FeedsContract.FeedsEntry.TABLE_NAME + "." + FeedsContract.FeedsEntry._ID,
            FeedsContract.FeedsEntry.COLUMN_USER,
            FeedsContract.FeedsEntry.COLUMN_POST_KEY,
            FeedsContract.FeedsEntry.COLUMN_THUMB,
    };
    static final int INDEX_FEED_ID = 0;
    static final int INDEX_USER_NAME = 1;
    static final int INDEX_FEEDS_THUMB = 4;
    static final int INDEX_FEED_KEY = 5;
@Override
            public void onDataSetChanged() {
                if (data != null) {
                    data.close();
                }
                final long identityToken = Binder.clearCallingIdentity();
                data = getContentResolver().query(FeedsContract.FeedsEntry.CONTENT_URI,
                        FEEDS_COLUMNS,
                        null,
                        null,
                        null);
                Binder.restoreCallingIdentity(identityToken);
            }
 @Override
            public RemoteViews getViewAt(int position) {
                if (position == AdapterView.INVALID_POSITION ||
                        data == null || !data.moveToPosition(position)) {
                    return null;
                }
                RemoteViews views = new RemoteViews(getPackageName(),
                        R.layout.widget_items);
                String thumbId = data.getString(INDEX_FEEDS_THUMB);
                Bitmap feedsImage = null;
                try {
                    feedsImage = Glide.with(DetailWidgetRemoteService.this)
                            .load(thumbId)
                            .asBitmap()
                            .error(R.drawable.person_placeholder)
                            .into(150, 150).get();
                } catch (InterruptedException | ExecutionException e) {
                    e.printStackTrace();
                }

这是我的合同类

public static final class FeedsEntry implements BaseColumns {
    public static final Uri CONTENT_URI =
            BASE_CONTENT_URI.buildUpon().appendPath(PATH_FEEDS).build();
    public static final String CONTENT_TYPE =
            ContentResolver.CURSOR_DIR_BASE_TYPE + "/" + CONTENT_AUTHORITY + "/" + PATH_FEEDS;
    public static final String CONTENT_ITEM_TYPE =
            ContentResolver.CURSOR_ITEM_BASE_TYPE + "/" + CONTENT_AUTHORITY + "/" + PATH_FEEDS;
    public static final String TABLE_NAME = "feeds";
    public static final String COLUMN_USER = "username";
    public static final String COLUMN_VIDEO = "video";
    public static final String COLUMN_TIME_STAMP = "timestamp";
    public static final String COLUMN_THUMB = "thumb";
    public static final String COLUMN_POST_KEY = "key";
    public static final String COLUMN_USER_PROFILE = "profile";

进行查询时,光标只返回查询列,因此,如果查询这些列:

static final int INDEX_FEED_ID = 0;
static final int INDEX_USER_NAME = 1;
static final int INDEX_FEEDS_THUMB = 4;
static final int INDEX_FEED_KEY = 5;

然后,您可以使用data.getString(index)从光标访问它们

INDEX_FEED_ID - index 0 
INDEX_USER_NAME - index 1
INDEX_FEEDS_THUMB - index 2
INDEX_FEED_KEY - index 3

最新更新