android sqlite cursor
I got these error when running the application
Unable to start activity ComponentInfo{com.rika/com.rika.LatihanActivity}:
android.database.StaleDataException: Access closed cursor
ERROR/AndroidRuntime(299): Caused by: android.database.StaleDataException: Access closed cursor
Is the error happened because of the cursor code?
Its the activity class
public class LatihanActivity extends Activity{
/** Called when the activity is first created. */
private RadioButton radioButton;
private TextView quizQuestion;
private int rowIndex = 1;
private int questNo=0;
private boolean checked=false;
private boolean flag=true;
private RadioGroup radioGroup;
String[] corrAns = new String[5];
final DatabaseHelper db = new DatabaseHelper(this);
Cursor c1;
Cursor c2;
Cursor c3;
int counter=1;
String label;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String options[] = new String[19];
// get reference to radio group in layout
final RadioGroup radiogroup = (RadioGroup) findViewById(R.id.rdbGp1);
// layout params to use when adding each radio button
LinearLayout.LayoutParams layoutParams = new RadioGroup.LayoutParams(
RadioGroup.LayoutParams.WRAP_CONTENT,
RadioGroup.LayoutParams.WRAP_CONTENT);
try {
db.createDataBase();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
c3 = db.getCorrAns();
for(c3.moveToFirst();!c3.isAfterLast(); c3.moveToNext())
{
for (int i=0;i<=4;i++)
{
//... get data from DB
corrAns[i]=c3.getString(0);}
}
//then you can close it
c3.close();
///////////////////////////////////////
radioGroup = (RadioGroup) findViewById(R.id.rdbGp1);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
for(int i=0; i<radiogroup.getChildCount() ; i++) {
RadioButton btn = (RadioButton) radioGroup.getChildAt(i);
String text;
if (btn.isPressed() && btn.isChecked() && questNo < 5)
{
Log.e("corrAns[questNo]",corrAns[questNo]);
if (corrAns[questNo].equals(btn.getText()) && flag==true)
{
flag=false;
checked = true;
}
else if(checked==true)
{
flag=true;
checked = false;
}
}
}
}
});
quizQuestion = (TextView) findViewById(R.id.TextView01);
displayQuestion();
/*Saves the selected values in the database on the save button*/
Button btnSave = (Button) findViewById(R.id.btnSave);
btnSave.setOnClickListener(btnSave_Listener);
}
/*Called when save button is clicked*/
private View.OnClickListener btnSave_Listener= new View.OnClickListener() {
@Override
public void onClick(View v) {
}
};
private void displayQuestion() {
//Fetching data quiz data and incrementing on each click
c1=db.getQuiz_Content(rowIndex);
c2 =db.getAns(rowIndex++);
quizQuestion.setText(c1.getString(0));
radioGroup.removeAllViews();
//***
if (c2.moveToFirst())
{
for (int i=0;i<=3;i++)
{
//Generating and adding 4 radio buttons dynamically
radioButton = new RadioButton(this);
radioButton.setText(c2.getString(0));
radioButton.setId(i);
c2.moveToNext();
radioGroup.addView(radioButton);
}
}
}}
Its the database helper class
public class DatabaseHelper extends SQLiteOpenHelper{
private static String DB_PATH = "/data/data/com.rika/databases/";
private static String DB_NAME = "test.sqlite";
private static String Table_name="Quiz";
private SQLiteDatabase myDataBase;
private SQLiteDatabase myData;
private final Context myContext;
public DatabaseHelper(Context context) {
super(context, DB_NAME, null, 1);
this.myContext = context;
}
public void createDataBase() throws IOException{
boolean dbExist = checkDataBase();
if(dbExist){
//do nothing - database already exist
}else{
CopyFiles();
}
}
private boolean checkDataBase() {
// TODO Auto-generated method stub
return false;
}
@SuppressWarnings("null")
private void CopyFiles() throws IOException
{
FileOutputStream myOutput = null;
try
{
InputStream is = myContext.getAssets().open(DB_NAME);
File outfile = new File(DB_PATH,DB_NAME);
outfile.getParentFile().mkdirs();
outfile.createNewFile();
if (is == null)
throw new RuntimeException("stream is null");
else
{
FileOutputStream out = new FileOutputStream(outfile);
// BufferedOutputStream out = new BufferedOutputStream( new FileOutputStream(outfile));
byte buf[] = new byte[128];
do {
int numread = is.read(buf);
if (numread <= 0) break; out.write(buf, 0, numread); } while (true); is.close(); out.close(); } //AssetFileDescriptor af = am.openFd("world_treasure_hunter_deluxe.apk"); } catch (IOException e) { throw new RuntimeException(e); } } /** * Check if the database already exist to avoid re-copying the file each time you open the application. * @return true if it exists, false if it doesn't */ private boolean checkDataBase(){ SQLiteDatabase checkDB = null; try{ String myPath = DB_PATH + DB_NAME; checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); }catch(SQLiteException e){ } if(checkDB != null){ checkDB.close(); } return checkDB != null ? true : false; } /** * Copies your database from your local assets-folder to the just created empty database in the * system folder, from where it can be accessed and handled. * This is done by transfering bytestream. * */ private void copyDataBase() throws IOException{ //Open your local db as the input stream InputStream myInput = myContext.getAssets().open(DB_NAME); // Path to the just created empty db String outFileName = DB_PATH + DB_NAME; //Open the empty db as the output stream OutputStream 开发者_运维技巧myOutput = new FileOutputStream(outFileName); //transfer bytes from the inputfile to the outputfile byte[] buffer = new byte[1024]; int length; while ((length = myInput.read(buffer))>0){
byte[] buffer = null;
int length = 0;
myOutput.write(buffer, 0, length);
} finally{}
//Close the streams
myOutput.flush();
myOutput.close();
FileOutputStream myInput = null;
myInput.close();
}
public void openDataBase() throws SQLException{
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}
@Override
public synchronized void close() {
if(myDataBase != null)
myDataBase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public Cursor getQuiz_Content(int bookId)
{
String myPath = DB_PATH + DB_NAME;
myData = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
Cursor cur;
cur=myData.rawQuery("select Quiz_text from Quiz where Quiz_id='"+bookId+"'",null);
cur.moveToFirst();
cur.moveToNext();
return cur;
};
public Cursor getQuiz_List()
{
String myPath = DB_PATH + DB_NAME;
myData = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
int i;
Cursor cur;
cur=myData.rawQuery("select Quiz_id,Quiz_text,Correct_Answer from Quiz",null);
cur.moveToFirst();
i = cur.getCount();
myData.close();
return cur;
};
public Cursor getAns(int quizid)
{
String myPath = DB_PATH + DB_NAME;
myData = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
Cursor cur;
cur = myData.rawQuery("select Answer from Answers where Quiz_id='"+quizid+"'", null);
cur.moveToFirst();
myData.close();
return cur;
}
public Cursor getAnsList()
{
String myPath = DB_PATH + DB_NAME;
myData = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
Cursor cur;
cur = myData.rawQuery("select Answer from Answers", null);
cur.moveToFirst();
cur.moveToNext();
myData.close();
return cur;
}
public Cursor getCorrAns()
{
String myPath = DB_PATH + DB_NAME;
myData = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
Cursor cur;
cur = myData.rawQuery("select Correct_Answer from Quiz", null);
cur.moveToFirst();
cur.moveToNext();
myData.close();
return cur;
}}
Probably you are closing your cursor before dealing with whatever you are trying to do.
Cursor cursor = getBaseContext().managedQuery(uri, null, null, null, null);
for(cursor.moveToFirst();!cursor.isAfterLast(); cursor.moveToNext())
{
//... get data from DB
}
//then you can close it
cursor.close();
Are you creating object of SQLiteDatabase class ---->
SQLiteDatabase db = SQLiteDatabaseOpenHelper class object.getWritableDatabase();
if not then try it, might solve your problem.
精彩评论