gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 20:54:27 29 4
gpt4 key购买 nike

我下载了一个 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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com