android的sqlite的游标 [英] android sqlite cursor
问题描述
在运行应用程序时,我得到了这些错误。
无法启动活动ComponentInfo {com.rika / com.rika.LatihanActivity}:
android.database.StaleDataException:访问关闭光标
ERROR / AndroidRuntime(299):android.database.StaleDataException:产生的原因访问关闭光标
时的错误发生,因为光标code?
它的活动类
公共类LatihanActivity延伸活动{
/ **第一次创建活动时调用。 * /
私人单选按钮单选按钮;
私人TextView的quizQuestion;
私人诠释rowIndex位置= 1;
私人诠释questNo = 0;
私人布尔查= FALSE;
私人布尔标志=真;
私人RadioGroup中RadioGroup中;
的String [] corrAns =新的String [5];
最后DatabaseHelper DB =新DatabaseHelper(本);
游标c1;
光标C2;
光标C3;
INT计数器= 1;
字符串标签;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
字符串选项[] =新的String [19];
//获取是无线电集团布局
最后RadioGroup中RadioGroup中=(RadioGroup中)findViewById(R.id.rdbGp1);
//布局PARAMS来增加每个单选按钮时使用
LinearLayout.LayoutParams的LayoutParams =新RadioGroup.LayoutParams(
RadioGroup.LayoutParams.WRAP_CONTENT,
RadioGroup.LayoutParams.WRAP_CONTENT);
尝试 {
db.createDataBase();
}赶上(IOException异常E){
// TODO自动生成的catch块
e.printStackTrace();
}
C3 = db.getCorrAns();
为(c3.moveToFirst();! c3.isAfterLast(); c3.moveToNext())
{
的for(int i = 0; I< = 4;我++)
{
// ...得到DB数据
corrAns [I] = c3.getString(0);}
}
//那么你可以将其关闭
c3.close();
///////////////////////////////////////
RadioGroup中=(RadioGroup中)findViewById(R.id.rdbGp1);
radioGroup.setOnCheckedChangeListener(新RadioGroup.OnCheckedChangeListener(){
@覆盖
公共无效onCheckedChanged(RadioGroup中组,诠释checkedId){
// TODO自动生成方法存根
的for(int i = 0; I< radiogroup.getChildCount();我++){
单选按钮BTN =(单选)radioGroup.getChildAt(我);
文本字符串;
如果(btn.is pressed()&安培;&安培; btn.isChecked()&安培;&安培; questNo小于5)
{
Log.e(corrAns [questNo],corrAns [questNo]);
如果(corrAns [questNo] .equals(btn.getText())及和放大器;标志==真)
{
标志= FALSE;
检查=真;
}
否则,如果(检查==真)
{
标志=真正的;
检查=虚假的;
}
}
}
}
});
quizQuestion =(TextView中)findViewById(R.id.TextView01);
displayQuestion();
/ *保存所选值在保存按钮数据库* /
按钮btnSave =(按钮)findViewById(R.id.btnSave);
btnSave.setOnClickListener(btnSave_Listener);
}
/ *当保存按钮被点击调用* /
私人View.OnClickListener btnSave_Listener =新View.OnClickListener(){
@覆盖
公共无效的onClick(视图v){
}
};
私人无效displayQuestion(){
//读取数据的测验数据,并递增每个点击
C1 = db.getQuiz_Content(rowIndex位置);
C2 = db.getAns(rowIndex位置++);
quizQuestion.setText(c1.getString(0));
radioGroup.removeAllViews();
// ***
如果(c2.moveToFirst())
{
的for(int i = 0; I< = 3;我++)
{
//生成和动态添加4单选按钮
单选按钮=新的单选按钮(这一点);
radioButton.setText(c2.getString(0));
radioButton.setId(ⅰ);
c2.moveToNext();
radioGroup.addView(单选按钮);
}
}
}}
它的数据库辅助类
公共类DatabaseHelper扩展SQLiteOpenHelper {
私有静态字符串DB_PATH =/data/data/com.rika/databases/;
私有静态字符串DB_NAME =test.sqlite;
私有静态字符串TABLE_NAME =测验;
私人SQLiteDatabase MyDatabase的;
私人SQLiteDatabase myData的;
私人最终语境myContext;
公共DatabaseHelper(上下文的背景下){
超级(上下文,DB_NAME,空,1);
this.myContext =背景;
}
公共无效的CreateDatabase()抛出IOException异常{
布尔dbExist = checkDataBase();
如果(dbExist){
//什么也不做 - 数据库中已存在
}其他{
CopyFiles();
}
}
私人布尔checkDataBase(){
// TODO自动生成方法存根
返回false;
}
@燮pressWarnings(空)
私人无效CopyFiles()抛出IOException异常
{
FileOutputStream中myOutput = NULL;
尝试
{
InputStream的是= myContext.getAssets()开(DB_NAME)。
文件OUTFILE =新的文件(DB_PATH,DB_NAME);
outfile.getParentFile()mkdirs()。
outfile.createNewFile();
如果(是== NULL)
抛出新的RuntimeException(流为空);
其他
{
FileOutputStream中出=新的FileOutputStream(OUTFILE);
//的BufferedOutputStream OUT =新的BufferedOutputStream(新的FileOutputStream(OUTFILE));
中byte buf [] =新的字节[128];
做 {
INT numread = is.read(BUF);
如果(numread< = 0)打破; out.write(BUF,0,numread); }而(真); is.close(); out.close(); } // AssetFileDescriptor AF = am.openFd(world_treasure_hunter_deluxe.apk); }赶上(IOException异常E){抛出新的RuntimeException(E); }} / ** *检查数据库中已存在,以避免重新复制每次打开应用程序时,该文件。 * @返回如果存在的话真的,假的,如果它不* /私营布尔checkDataBase(){SQLiteDatabase CHECKDB = NULL;尝试{字符串mypath中= DB_PATH + DB_NAME; CHECKDB = SQLiteDatabase.openDatabase(mypath中,空,SQLiteDatabase.OPEN_READONLY); }赶上(SQLiteException E){}如果(CHECKDB!= NULL){checkDB.close(); }返回CHECKDB!= NULL?真假; } / ** *副本从当地的资源文件夹数据库来在*系统文件夹中刚刚创建的空数据库,从那里可以访问和处理。 *这是通过transfering字节流进行。 * * /私人无效copyDataBase()抛出IOException异常{//打开本地数据库作为输入流的InputStream myInput = myContext.getAssets()开(DB_NAME)。 //路径刚刚创建的空数据库字符串outFileName = DB_PATH + DB_NAME; //打开空分贝的输出流的OutputStream myOutput =新的FileOutputStream(outFileName); //传输的字节从inputfile中的OUTPUTFILE byte []的缓冲区=新的字节[1024]; INT长;而((长度= myInput.read(缓冲液))大于0){
byte []的缓冲区= NULL;
INT长度= 0;
myOutput.write(缓冲液,0,长度);
} 最后{}
//关闭流
myOutput.flush();
myOutput.close();
FileOutputStream中myInput = NULL;
myInput.close();
}
公共无效的openDatabase()抛出的SQLException {
字符串mypath中= DB_PATH + DB_NAME;
MyDatabase的= SQLiteDatabase.openDatabase(mypath中,空,SQLiteDatabase.OPEN_READONLY);
}
@覆盖
市民同步无效的close(){
如果(MyDatabase的!= NULL)
myDataBase.close();
super.close();
}
@覆盖
公共无效的onCreate(SQLiteDatabase DB){
}
@覆盖
公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页){
}
公共光标getQuiz_Content(INT BOOKID)
{
字符串mypath中= DB_PATH + DB_NAME;
myData的= SQLiteDatabase.openDatabase(mypath中,空,SQLiteDatabase.OPEN_READONLY);
光标CUR;
CUR = myData.rawQuery(从测验选择Quiz_text,其中Quiz_id ='+ BOOKID +',NULL);
cur.moveToFirst();
cur.moveToNext();
返回CUR;
};
公共光标getQuiz_List()
{
字符串mypath中= DB_PATH + DB_NAME;
myData的= SQLiteDatabase.openDatabase(mypath中,空,SQLiteDatabase.OPEN_READONLY);
INT I;
光标CUR;
CUR = myData.rawQuery(选择Quiz_id,Quiz_text,Correct_Answer从测验,NULL);
cur.moveToFirst();
I = cur.getCount();
myData.close();
返回CUR;
};
公共光标getAns(INT quizid)
{
字符串mypath中= DB_PATH + DB_NAME;
myData的= SQLiteDatabase.openDatabase(mypath中,空,SQLiteDatabase.OPEN_READONLY);
光标CUR;
CUR = myData.rawQuery(选择答案答案在Quiz_id ='+ quizid +',NULL);
cur.moveToFirst();
myData.close();
返回CUR;
}
公共光标getAnsList()
{
字符串mypath中= DB_PATH + DB_NAME;
myData的= SQLiteDatabase.openDatabase(mypath中,空,SQLiteDatabase.OPEN_READONLY);
光标CUR;
CUR = myData.rawQuery(选择回答,NULL);
cur.moveToFirst();
cur.moveToNext();
myData.close();
返回CUR;
}
公共光标getCorrAns()
{
字符串mypath中= DB_PATH + DB_NAME;
myData的= SQLiteDatabase.openDatabase(mypath中,空,SQLiteDatabase.OPEN_READONLY);
光标CUR;
CUR = myData.rawQuery(选择Correct_Answer从测验,NULL);
cur.moveToFirst();
cur.moveToNext();
myData.close();
返回CUR;
}}
也许你关闭你的光标处理无论你正在尝试做了。
光标光标= getBaseContext()managedQuery(URI,NULL,NULL,NULL,NULL);
为(cursor.moveToFirst();! cursor.isAfterLast(); cursor.moveToNext())
{
// ...得到DB数据
}
//那么你可以将其关闭
cursor.close();
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();
这篇关于android的sqlite的游标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!