gpt4 book ai didi

android sqlite游标

转载 作者:行者123 更新时间:2023-11-29 02:10:35 26 4
gpt4 key购买 nike

运行应用程序时出现这些错误

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

是游标代码导致的错误吗?

是 Activity 类

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);
}
}


}}

它是数据库助手类

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;
}}

最佳答案

可能在处理任何您尝试做的事情之前,您正在关闭光标。

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();

关于android sqlite游标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7685563/

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