我正在以下代碼中訪問來自sqlite數據庫的數據。完成尚未停用或關閉的遊標
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.profile);
type_spn = (Spinner) findViewById(R.id.type_spn);
animal_spn = (Spinner) findViewById(R.id.animal_spn);
habitat_txt=(TextView)findViewById(R.id.life_txt);
diet_txt=(TextView)findViewById(R.id.habit_txt);
discription_txt=(TextView)findViewById(R.id.description_txt);
adb = DBAdpter.getAdapterInstance(SecondActivity.this);
adb.createdatabase();
db = adb.openDataBase();
cr = db.rawQuery("select distinct type from zoo", new String[] {});
if (cr.getCount() > 0) {
cr.moveToFirst();
for (int i = 0; i < cr.getCount(); i++) {
String type = cr.getString(0);
cr.moveToNext();
type_list.add(type);
}
db.close();
ArrayAdapter<String> type_add = new ArrayAdapter<String>(
SecondActivity.this, R.layout.spinnerlayout, type_list);
type_spn.setAdapter(type_add);
}
type_spn.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
animal_list.clear();
String type_name = arg0.getItemAtPosition(arg2).toString();
db = adb.openDataBase();
cr = db.rawQuery("select animal from zoo where type like '"
+ type_name + "%'", new String[] {});
if (cr.getCount() > 0) {
cr.moveToFirst();
for (int i = 0; i < cr.getCount(); i++) {
String type = cr.getString(0);
cr.moveToNext();
animal_list.add(type);
}
}
db.close();
ArrayAdapter<String> type_add = new ArrayAdapter<String>(
SecondActivity.this, R.layout.spinnerlayout,
animal_list);
animal_spn.setAdapter(type_add);
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
animal_spn.setOnItemSelectedListener(new OnItemSelectedListener() {
String habitat;
String diet;
String description;
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
String animal_name = arg0.getItemAtPosition(arg2).toString();
db = adb.openDataBase();
cr = db.rawQuery(
"select habitat,diet,discription from zoo where type like '"
+ animal_name + "%'", new String[] {});
if (cr.getCount() > 0) {
Log.v("anim","Test"+cr.getCount());
cr.moveToFirst();
habitat =cr.getString(0);
diet =cr.getString(1);
description =cr.getString(2);
}
db.close();
Log.v("anim","Test"+" : "+habitat+" : "+diet+" : " + description);
habitat_txt.setText(habitat);
diet_txt.setText(diet);
discription_txt.setText(description);
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
我得到這樣的錯誤。
03-28 18:08:09.356: ERROR/Cursor(4754): Finalizing a Cursor that has not been deactivated or closed. database = /data/data/com.zoobuzz/databases/zoo_buzz.sqlite, table = null, query = select animal from zoo where type like 'Bird
03-28 18:08:09.356: ERROR/Cursor(4754): %'
03-28 18:08:09.356: ERROR/Cursor(4754): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
03-28 18:08:09.356: ERROR/Cursor(4754): at android.database.sqlite.SQLiteCursor.<init>(SQLiteCursor.java:210)
03-28 18:08:09.356: ERROR/Cursor(4754): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:53)
03-28 18:08:09.356: ERROR/Cursor(4754): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1345)
03-28 18:08:09.356: ERROR/Cursor(4754): at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1315)
03-28 18:08:09.356: ERROR/Cursor(4754): at com.zoobuzz.SecondActivity$1.onItemSelected(SecondActivity.java:65)
03-28 18:08:09.356: ERROR/Cursor(4754): at android.widget.AdapterView.fireOnSelected(AdapterView.java:864)
03-28 18:08:09.356: ERROR/Cursor(4754): at android.widget.AdapterView.access$200(AdapterView.java:42)
03-28 18:08:09.356: ERROR/Cursor(4754): at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:830)
03-28 18:08:09.356: ERROR/Cursor(4754): at android.os.Handler.handleCallback(Handler.java:587)
03-28 18:08:09.356: ERROR/Cursor(4754): at android.os.Handler.dispatchMessage(Handler.java:92)
03-28 18:08:09.356: ERROR/Cursor(4754): at android.os.Looper.loop(Looper.java:123)
03-28 18:08:09.356: ERROR/Cursor(4754): at android.app.ActivityThread.main(ActivityThread.java:4627)
03-28 18:08:09.356: ERROR/Cursor(4754): at java.lang.reflect.Method.invokeNative(Native Method)
03-28 18:08:09.356: ERROR/Cursor(4754): at java.lang.reflect.Method.invoke(Method.java:521)
03-28 18:08:09.356: ERROR/Cursor(4754): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-28 18:08:09.356: ERROR/Cursor(4754): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-28 18:08:09.356: ERROR/Cursor(4754): at dalvik.system.NativeStart.main(Native Method)
謝謝hotveryspicy這對我來說非常有幫助。 – 2012-03-28 13:14:57
+1它可以幫助我@hotveryspicy – Sathish 2013-03-23 07:12:32