好吧,我是android开发的新手,或者任何开发和获取此错误的新手。我想做的是每次用户选择一个微调项目,然后程序创建一个列表。我有一个DatabaseHelper类,我用它来获取列表的结果。但问题是它永远不会创建一个列表,它会给出这个错误。
DatabaseHelper活动使用的方法;
public int[] searchbyLetter(String letter) {
// TODO Auto-generated method stub
int[] results = new int[100];
int count = 0;
String[] columns = new String[] { KEY_MINERALID };
Cursor c = minerals.query(DATABASE_TABLE, columns, KEY_LETTER + "= '"
+ letter + "'", null, null, null, null);
if (c.moveToFirst()) {
results[count] = c.getInt(c.getColumnIndex(KEY_MINERALID));
}
while (c.moveToNext()) {
count++;
results[count] = c.getInt(c.getColumnIndex(KEY_MINERALID));
}
c.close();
return results;
}
public String getName(int _id) {
// TODO Auto-generated method stub
String[] columns = new String[] { KEY_NAME };
Cursor c = minerals.query(DATABASE_TABLE, columns, KEY_MINERALID + "="
+ _id, null, null, null, null);
c.moveToFirst();
int iName = c.getColumnIndex(KEY_NAME);
String result = c.getString(iName);
c.close();
return result;
}
这是我的ListActivity类;
public class Az extends ListActivity {
String[] spAzPaht = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
"K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W",
"Y", "Z" };
ArrayList<AzOrder> azOrder;
AzOrderAdapter azOrderAdapter;
Spinner spAZ;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.az);
ArrayAdapter<String> spAzAdpt = new ArrayAdapter<String>(Az.this,
android.R.layout.simple_spinner_item, spAzPaht);
spAZ = (Spinner) findViewById(R.id.spAZ);
spAZ.setAdapter(spAzAdpt);
azOrder = new ArrayList<AzOrder>();
this.azOrderAdapter = new AzOrderAdapter(Az.this, R.layout.az, azOrder);
setListAdapter(azOrderAdapter);
spAZ.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
getSearches();
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
getSearches();
}
});
}
private void getSearches() {
DatabaseHelper myDBHelper = new DatabaseHelper(Az.this);
try {
myDBHelper.createDataBase();
} catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}
try {
myDBHelper.openDataBase();
} catch (SQLException sqle) {
throw sqle;
// TODO: handle exception
}
int position = spAZ.getSelectedItemPosition();
String letter = spAzPaht[position];
int[] results = myDBHelper.searchbyLetter(letter);
try {
azOrder = new ArrayList<AzOrder>();
AzOrder AZO = new AzOrder();
for (int i = 0; i < results.length; i++) {
AZO.setName(myDBHelper.getName(results[i]));
AZO.setMineral_ID(results[i]);
azOrder.add(AZO);
}
Thread.sleep(5000);
Log.i("Array", "" + azOrder.size());
} catch (Exception e) {
// TODO: handle exception
Log.e("BACKGROUND_PROC", e.getMessage());
}
myDBHelper.close();
}
这是我得到的错误
E/Cursor(295): Finalizing a Cursor that has not been deactivated or
closed. database = /data/data/com.mineralidentifier.degosa.test1/databases/
minerals, table = minerals, query = SELECT minerals_name FROM minerals
WHERE _id=0
E/Cursor(295): android.database.sqlite.DatabaseObjectNotClosedException:
Application did not close the cursor or database object that was opened
here
E/Cursor(295): at android.database.sqlite.SQLiteCursor.<init>
(SQLiteCursor.java:210)
E/Cursor(295): at android.database.sqlite.SQLiteDirectCursorDriver.query
(SQLiteDirectCursorDriver.java:53)
答案 0 :(得分:1)
看起来Cursor没有被关闭。我看到你有c.close(),但可能无法访问它,因为在它之前抛出了另一个异常。并且,该异常被DatabaseObjectNotClosedException屏蔽,最终被抛出屏蔽原始异常。最好将它包装在finally子句中,以确保它始终是关闭的,无论是否抛出异常。
Cursor c = ...
try {
...
} finally {
c.close();
}
如果你这样做,它可能会告诉你真正的问题是什么。不幸的是,由于我们没有看到完整的异常堆栈跟踪,因此我们无法在代码中看到引发DatabaseObjectNotClosedException的位置。我们只能看到它抛出的android代码中的哪个位置,但我们看不到过去的SQLiteDirectCursorDriver.query。