mardi 27 janvier 2015

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

I downloaded an android sqlite project to get me started as I am completely new. So when I run my app it loads perfectly but when I go to load it up again it says it has stopped working. The main error on logcat is



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


How can I fix this error? Thank you


Here is the sample code I downloaded: 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";

}

Aucun commentaire:

Enregistrer un commentaire