IllegalStateException异常

时间:2012-12-05 16:57:17

标签: android android-cursor

我有一个预算应用程序,当我试图从我的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;
            }
        }

这是我的logcat

 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

2 个答案:

答案 0 :(得分:3)

您的光标投影只有一列KEY_MEMO

但您尝试使用

访问第二列
i.getString(1)

使用

i.getString(0)

代替

答案 1 :(得分:3)

光标只包含一列。使用

Memos.add(i.getString(0));