java.lang.IllegalStateException:无法从CursorWindow读取第0行col -1。

时间:2015-03-14 19:46:15

标签: android sql

我的代码中有一个java.lang.IllegalStateException问题。我已经阅读了一些以前的答案等,但似乎没有任何工作。

这是我的代码:

    highFreqWord = (TextView) findViewById(R.id.freqWord);
    //Instantiate database connection
    mydb = new DBHelper(this);

    Bundle extras = getIntent().getExtras();

    Log.d("extraKDJFSKDF", String.valueOf(extras));
    //If there is extras a contact has been selected. If not we are processing an Add Contact request from the main menu
    if(extras !=null)
    {
        Log.d("Hit,", "id");
        int value = extras.getInt("id");
        Log.d("extraKDJFSKDF", String.valueOf(extras));
        Log.d("id", String.valueOf(value));
        if(value>0){
            //Retrieve the data back from the database for the selected contact
            Cursor rs = mydb.getData(value);
            id_To_Update = value;
            //Move to first item in query result from mydb.getData() method
            rs.moveToFirst();
            Log.d("Hit ","1");
            //Setting string values to appropriate data in database result (rs)
            String freqUsedWord = rs.getString(rs.getColumnIndex(DBHelper.FREQ_WORD));
            if (!rs.isClosed())
            {
                rs.close();
            }

以及我得到的错误:

 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ltssdyslexiaapp/com.example.ltss.dyslexia.app.FreqWordsPage}: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
        at android.app.ActivityThread.access$800(ActivityThread.java:135)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5001)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        at dalvik.system.NativeStart.main(Native Method)

这是我创建表格的DBHelper代码:

String CREATE_FREQ_WORDS_TABLE  = "CREATE TABLE " + TABLE_FREQ_WORDS  + "("
            + FREQ_WORD_ID + " INTEGER PRIMARY KEY," + FREQ_WORD  + " TEXT" + ")";
    db.execSQL(CREATE_FREQ_WORDS_TABLE); 

我已经尝试了我能想到的一切,但无济于事 - 有没有办法获得更有用的错误?

任何建议都会非常感激。谢谢:)

2 个答案:

答案 0 :(得分:0)

简单的错误,你必须在分配之前先将highFreqWord初始化为textview。

Textview highFreqWord = (Textview) findViewbyId(R.idfreqWord);

答案 1 :(得分:0)

没关系 - 事实证明我正在调用不正确的getData游标。一旦修复并重新安装了应用程序,它就会正常运行

感谢您的帮助:)