java.lang.IllegalStateException:无法从CursorWindow读取第0行,第0列

时间:2017-01-07 08:26:56

标签: java android sqlite illegalstateexception

我需要从表中获取所有数据,我使用以下代码在sqlite dbhelper类中执行相同操作:

public Cursor getData() {
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor res = db.rawQuery("select * from " + PRODUCT_DETAILS_TABLE, null);
        return res;
    }

现在,我通过以下方式使用光标在我的活动中获取数据:

  public HashMap<String, ArrayList<CartModal>> getDatas() {

            map = new HashMap<String, ArrayList<CartModal>>();
            stored_data = new ArrayList<CartModal>();
            Cursor cartdb = dbHelper.getData();
            if (cartdb != null && cartdb.getCount() > 0) {
                if (cartdb.moveToFirst()) {
                    do {
                        String mloggedinuser = cartdb.getString(0);
                        String mname = cartdb.getString(1);
                        String email = cartdb.getString(2);
                        String mobile = cartdb.getString(3);
                        String about = cartdb.getString(4);
                        String pid = cartdb.getString(5);
                        String refnum = cartdb.getString(6);
                        String size = cartdb.getString(7);
                        String weight = cartdb.getString(8);
                        String price = cartdb.getString(9);
                        String catid = cartdb.getString(10);
                        String image = cartdb.getString(11);

                        CartModal cartarray = new CartModal(mloggedinuser, mname, email, mobile, about, pid, refnum, size, weight, price, catid, image);
                        stored_data.add(cartarray);
                        keys = mname;
                        map.put(mname, stored_data);
                    } while (cartdb.moveToPosition(stored_data.size()));
                    cartdb.close();
                }
            }
            return map;
        }

当我运行上述程序时,我收到如下错误:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: sdetech.io.brouchuretabproject, PID: 2434
                  java.lang.IllegalStateException: Couldn't read row 0, col 0 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 sdetech.io.brouchuretabproject.Fragments.Cart.getDatas(Cart.java:58)
                      at sdetech.io.brouchuretabproject.Fragments.Cart.onCreateView(Cart.java:42)
                      at android.support.v4.app.Fragment.performCreateView(Fragment.java:2087)
                      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1113)
                      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1295)
                      at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801)
                      at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1682)
                      at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:541)
                      at android.os.Handler.handleCallback(Handler.java:739)
                      at android.os.Handler.dispatchMessage(Handler.java:95)
                      at android.os.Looper.loop(Looper.java:148)
                      at android.app.ActivityThread.main(ActivityThread.java:5417)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

我在stackoverflow(android java.lang.IllegalStateException: Couldn't read row 0, col 0 from CursorWindow)中经历了很多解决方案,但没有人帮助我解决这个问题。 请给我一个解决方案。

1 个答案:

答案 0 :(得分:0)

我正在做的一切正确。但问题是,我在db中将图像保存为base64 ecoded string。虽然获取该数据它显示非法异常。当我删除该图像时,它完美地工作。 如果有人知道如何在sqlite中将base64编码的图像保存为字符串,请在下面回复。

三江源