java.lang.IllegalStateException : Trying to requery an already closed cursor error 错误

标签 java android eclipse sqlite

我下载了一个 android sqlite 项目来帮助我入门,因为我是全新的。因此,当我运行我的应用程序时,它可以完美加载,但是当我再次加载它时,它说它已停止工作。 logcat 的主要错误是

java.lang.RuntmeException: Unable to resume activity(...MainActivity): java.lang.IllegalStateException: Trying to requery an already closed cursor android.database.sqlite.SQLiteCursor@41b80fb0

如何解决这个错误?谢谢

这是我下载的示例代码: 主 Activity .java

package com.example.events;

import android.app.ListActivity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import com.example.events.Constants;
import static android.provider.BaseColumns._ID;
import static com.example.events.Constants.TABLE_NAME;
import static com.example.events.Constants.TIME;
import static com.example.events.Constants.TITLE;


public class MainActivity extends ListActivity {

    private EventsData events;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        events = new EventsData(this);
        try {
        addEvent("Hello, Android!");
        Cursor cursor = getEvents();
         showEvents(cursor);
        } finally {
         events.close();
         }
        }
    private void addEvent(String string) {
        // Insert a new record into the Events data source.
        // You would do something similar for delete and update.
        SQLiteDatabase db = events.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(TIME, System.currentTimeMillis());
        values.put(TITLE, string);
        db.insertOrThrow(TABLE_NAME, null, values);
        }

    private static String[] FROM = { _ID, TIME, TITLE, };
    private static String ORDER_BY = TIME + " DESC";
    private Cursor getEvents() {
    // Perform a managed query. The Activity will handle closing
    // and re-querying the cursor when needed.
    SQLiteDatabase db = events.getReadableDatabase();
    Cursor cursor = db.query(TABLE_NAME, FROM, null, null, null,
    null, ORDER_BY);
    startManagingCursor(cursor);
    return cursor;
    }
    private static int[] TO = { R.id.rowid, R.id.time, R.id.title, };
    private void showEvents(Cursor cursor) {
         // Stuff them all into a big string
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
                R.layout.item, cursor, FROM, TO);
                setListAdapter(adapter);
        // StringBuilder builder = new StringBuilder(
        // "Saved events:\n");
        // while (cursor.moveToNext()) {
         // Could use getColumnIndexOrThrow() to get indexes
        // long id = cursor.getLong(0);
        // long time = cursor.getLong(1);
        // String title = cursor.getString(2);
        // builder.append(id).append(": ");
        // builder.append(time).append(": ");
        // builder.append(title).append("\n");
        // }
         // Display on the screen
        // TextView text = (TextView) findViewById(R.id.text);
        // text.setText(builder);
         }






    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

事件数据.java

package com.example.events;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import com.example.events.Constants;
import static android.provider.BaseColumns._ID;
import static com.example.events.Constants.TABLE_NAME;
import static com.example.events.Constants.TIME;
import static com.example.events.Constants.TITLE;

public class EventsData extends SQLiteOpenHelper {


    private static final String DATABASE_NAME = "events.db";
    private static final int DATABASE_VERSION = 1;


    public EventsData(Context ctx) {
         super(ctx, DATABASE_NAME, null, DATABASE_VERSION);
        }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE " + TABLE_NAME + " (" + _ID
                 + " INTEGER PRIMARY KEY AUTOINCREMENT, " + TIME
                 + " INTEGER," + TITLE + " TEXT NOT NULL);");

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);


    }

}

常量.java

package com.example.events;

import android.provider.BaseColumns;

    public interface Constants extends BaseColumns {

        public static final String TABLE_NAME = "events";
        // Columns in the Events database
        public static final String TIME = "time";
        public static final String TITLE = "title";

    }

最佳答案

startManagingCursor 方法在 API 级别 11 中被弃用。使用新的 CursorLoader 类和 LoaderManager 代替;这也可以通过 Android 兼容包在旧平台上使用。

解决方案一

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
        startManagingCursor(Cursor);
    }

如果您正在使用 startManagingCursor(),您还应该调用 stopManagingCursor()

方案二

删除方法调用本身。

关于java.lang.IllegalStateException : Trying to requery an already closed cursor error 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28180838/

相关文章:

android - 构建指纹 : 'samsung/GT-P7500/GT-P7500:3.2/HTJ85B/XWKL1:user/release-keys' error in Android

java - 如何在一个textView中显示多个变量

java - 如何在 Eclipse 中使用 JavaPOS 进行开发?

java - 如何为 sts 3.5.0 配置 8GB 内存的 sts.ini 文件?

java - Eclipse 中的状态监视器?

java - 如何查找特定 smtp.host 的 SMTP 端口?

java Akka 。如何将参与者的行为与许多泛型相匹配

android - Android SDK 下载缓存文件夹在哪里?

java - 使用抽象方法重写调用私有(private)变量

java - Akka java 永远不会关闭 ActorRef