我下载了一个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
如何修复此错误?谢谢
以下是我下载的示例代码: MainActivity.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);
}
}
EventsData.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);
}
}
Constants.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";
}
答案 0 :(得分:2)
startManagingCursor
方法。请使用新的CursorLoader
类,而不是LoaderManager
;这也可以通过Android兼容包在旧版平台上使用。
解决方案1
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
startManagingCursor(Cursor);
}
如果您使用stopManagingCursor()
startManagingCursor()
解决方案2
删除方法调用本身。