【问题标题】:IllegalStateException: SQLite cursor already closedIllegalStateException:SQLite 游标已关闭
【发布时间】:2012-11-21 18:21:57
【问题描述】:

下面我发布 logcat 详细信息。请告诉我如何解决。

11-21 14:49:33.604: E/AndroidRuntime(1721): FATAL EXCEPTION: main
11-21 14:49:33.604: E/AndroidRuntime(1721): java.lang.RuntimeException: Unable to resume activity {com.example.shaktitool/com.example.shaktitool.Product}: java.lang.IllegalStateException: trying to requery an already closed cursor  android.database.sqlite.SQLiteCursor@41334490
11-21 14:49:33.604: E/AndroidRuntime(1721):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2575)
11-21 14:49:33.604: E/AndroidRuntime(1721):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2603)
11-21 14:49:33.604: E/AndroidRuntime(1721):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
11-21 14:49:33.604: E/AndroidRuntime(1721):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-21 14:49:33.604: E/AndroidRuntime(1721):     at android.os.Looper.loop(Looper.java:137)
11-21 14:49:33.604: E/AndroidRuntime(1721):     at android.app.ActivityThread.main(ActivityThread.java:4745)
11-21 14:49:33.604: E/AndroidRuntime(1721):     at java.lang.reflect.Method.invokeNative(Native Method)
11-21 14:49:33.604: E/AndroidRuntime(1721):     at java.lang.reflect.Method.invoke(Method.java:511)
11-21 14:49:33.604: E/AndroidRuntime(1721):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-21 14:49:33.604: E/AndroidRuntime(1721):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-21 14:49:33.604: E/AndroidRuntime(1721):     at dalvik.system.NativeStart.main(Native Method)
11-21 14:49:33.604: E/AndroidRuntime(1721): Caused by: java.lang.IllegalStateException: trying to requery an already closed cursor  android.database.sqlite.SQLiteCursor@41334490
11-21 14:49:33.604: E/AndroidRuntime(1721):     at android.app.Activity.performRestart(Activity.java:5051)
11-21 14:49:33.604: E/AndroidRuntime(1721):     at android.app.Activity.performResume(Activity.java:5074)
11-21 14:49:33.604: E/AndroidRuntime(1721):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2565)
11-21 14:49:33.604: E/AndroidRuntime(1721):     ... 10 more

我的 Java 代码:

package com.example.shaktitool;
    import android.app.Activity;
    import android.content.Intent;
    import android.database.Cursor;
    import android.os.Bundle;
    import android.view.KeyEvent;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemClickListener;
    import android.widget.ListView;
    import android.widget.SimpleCursorAdapter;



    public class Product extends Activity {

    protected static final String PRODUCT_REQUEST = null;

    private SQLiteAdapterPro mySQLiteAdapter;

        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.product);

            Intent intent = getIntent();

            ListView list = (ListView)findViewById(R.id.content_list);


            /*
             *  Create/Open a SQLite database
             *  and fill with dummy content
             *  and close it
             */
            mySQLiteAdapter = new SQLiteAdapterPro(this);
            mySQLiteAdapter.openToWrite();
            mySQLiteAdapter.deleteAll();

            mySQLiteAdapter.insert("ELECTRONICS-COMPUTERS");
            mySQLiteAdapter.insert("DESKTOPS");
            mySQLiteAdapter.insert("NOTEBOOKS");

            mySQLiteAdapter.close();

            /*
             *  Open the same SQLite database
             *  and read all it's content.
             */
            mySQLiteAdapter = new SQLiteAdapterPro(this);
            mySQLiteAdapter.openToRead();

            Cursor cursor = mySQLiteAdapter.queueAll();
            startManagingCursor(cursor);

            String[] from = new String[]{SQLiteAdapter.KEY_CONTENT};
            int[] to = new int[]{R.id.text};

            SimpleCursorAdapter cursorAdapter =
                new SimpleCursorAdapter(this, R.layout.row, cursor, from, to);

            list.setAdapter(cursorAdapter);

            list.setOnItemClickListener( new OnItemClickListener() {

                public void onItemClick(AdapterView<?> arg0, View arg1,
                        int position, long arg3) {

                    switch(position){

                    case 1 : Intent intent = new Intent(Product.this, Desktop.class);
                             startActivity(intent);

                             break;

                    case 2 : Intent intent1 = new Intent(Product.this, Notebook.class);
                             startActivity(intent1);

                             break;                        


                    }
                }   
            });


            mySQLiteAdapter.close();



        }
    }

【问题讨论】:

  • 问题是当我按下返回按钮时,应用程序关闭了。当我按下返回按钮时我应该怎么做,我必须去上一个活动。
  • 你有同样的解决方案吗?
  • 这很常见,请检查您的代码 游标已关闭或向此适配器提供了新游标,因此此异常即将到来,您可以避免调用此 mySQLiteAdapter.close(); (不确定适配器代码),第二件事你可以发布适配器代码然后我可以告诉根本原因..

标签: android sqlite cursor


【解决方案1】:

试图重新查询一个已经关闭的游标 android.database.sqlite.SQLiteCursor@41334490

您正在尝试访问已关闭的光标。

如果您仍然需要游标,请不要关闭它或进行新的数据库查询以获取另一个。

【讨论】:

    【解决方案2】:

    错误很明显。

    java.lang.IllegalStateException: 试图重新查询一个已经关闭的 光标。

    很难说你是如​​何对已经关闭的游标进行排队的。 但一种猜测是您可能正在使用 startManagingCursor() 并传递它已经关闭的游标。

    【讨论】:

      【解决方案3】:

      首先:您应该阅读 StackOverflow 的常见问题解答。

      第二:通过阅读您的 logcat 我了解:

      java.lang.RuntimeException: Unable to resume activity {com.example.shaktitool/com.example.shaktitool.Product}: java.lang.IllegalStateException: trying to requery an already closed cursor  android.database.sqlite.SQLiteCursor@41334490
      
      Caused by: java.lang.IllegalStateException: trying to requery an already closed cursor  android.database.sqlite.SQLiteCursor@41334490
      

      所以: trying to requery an already closed cursor 表示您使用 SQLite 做错了什么。发布您的 java 代码,您将获得帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-05-04
        • 1970-01-01
        • 2023-03-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-28
        相关资源
        最近更新 更多