-1

我有一个预算应用程序,当我试图从我的 SQLite 数据库中获取信息时,我不断收到IllegalStateExpeaction错误,我无法找出原因。这是我的代码,它在Memos.add(i.getString(1));发生错误的行

public List<String> getMemo(List<String> expeances) {
            // TODO Auto-generated method stub
            List<String> Memos = new ArrayList<String>();
            for(String exp: expeances)
            {
                Cursor i = ourDatabase.query(DATABASE_TABLE4, new String[]{KEY_MEMO},KEY_DATE+" = '"+exp+"'", null, null, null, null,null);

                if(i.getCount()>0)
                {
                    if(i.moveToFirst())
                    {
                        do 
                        {
                            Memos.add(i.getString(1));
                        } 
                        while (i.moveToNext());
                    }
                }
            }

            if (Memos.size()>0)
            {
                return Memos;
            }
            else
            {
                return null;
            }
        }

这是我的日志猫

 12-05 10:49:46.058: E/AndroidRuntime(12980): FATAL EXCEPTION: main
        12-05 10:49:46.058: E/AndroidRuntime(12980): java.lang.RuntimeException: Unable to start activity ComponentInfo{budget.app/budget.app.ReviewBudget}: java.lang.IllegalStateException: Couldn't read row 0, col 1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
        12-05 10:49:46.058: E/AndroidRuntime(12980):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
        12-05 10:49:46.058: E/AndroidRuntime(12980):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
        12-05 10:49:46.058: E/AndroidRuntime(12980):    at android.app.ActivityThread.access$600(ActivityThread.java:130)
        12-05 10:49:46.058: E/AndroidRuntime(12980):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
        12-05 10:49:46.058: E/AndroidRuntime(12980):    at android.os.Handler.dispatchMessage(Handler.java:99)
        12-05 10:49:46.058: E/AndroidRuntime(12980):    at android.os.Looper.loop(Looper.java:137)
        12-05 10:49:46.058: E/AndroidRuntime(12980):    at android.app.ActivityThread.main(ActivityThread.java:4745)
        12-05 10:49:46.058: E/AndroidRuntime(12980):    at java.lang.reflect.Method.invokeNative(Native Method)
        12-05 10:49:46.058: E/AndroidRuntime(12980):    at java.lang.reflect.Method.invoke(Method.java:511)
        12-05 10:49:46.058: E/AndroidRuntime(12980):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
        12-05 10:49:46.058: E/AndroidRuntime(12980):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
        12-05 10:49:46.058: E/AndroidRuntime(12980):    at dalvik.system.NativeStart.main(Native Method)
        12-05 10:49:46.058: E/AndroidRuntime(12980): 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.
        12-05 10:49:46.058: E/AndroidRuntime(12980):    at android.database.CursorWindow.nativeGetString(Native Method)
        12-05 10:49:46.058: E/AndroidRuntime(12980):    at android.database.CursorWindow.getString(CursorWindow.java:434)
        12-05 10:49:46.058: E/AndroidRuntime(12980):    at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
        12-05 10:49:46.058: E/AndroidRuntime(12980):    at budget.app.BudgetDB.getMemo(BudgetDB.java:511)
        12-05 10:49:46.058: E/AndroidRuntime(12980):    at budget.app.ReviewBudget.loadSpinnerData(ReviewBudget.java:58)
        12-05 10:49:46.058: E/AndroidRuntime(12980):    at budget.app.ReviewBudget.onCreate(ReviewBudget.java:51)
        12-05 10:49:46.058: E/AndroidRuntime(12980):    at android.app.Activity.performCreate(Activity.java:5008)
        12-05 10:49:46.058: E/AndroidRuntime(12980):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
        12-05 10:49:46.058: E/AndroidRuntime(12980):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
        12-05 10:49:46.058: E/AndroidRuntime(12980):    ... 11 more
4

2 回答 2

3

您的光标投影只有一列KEY_MEMO

但您正在尝试访问第二列

i.getString(1)

采用

i.getString(0)

反而

于 2012-12-05T17:04:33.177 回答
3

光标只包含一列。所以使用

Memos.add(i.getString(0));
于 2012-12-05T17:05:04.843 回答