gpt4 book ai didi

android - 完成尚未停用或关闭的 Cursor

转载 作者:行者123 更新时间:2023-11-30 04:23:37 25 4
gpt4 key购买 nike

好的,我是 android 开发的新手,或者任何开发并遇到此错误的新手。我想做的是每次用户选择一个微调项然后程序创建一个列表。我有一个用于获取列表结果的 DatabaseHelper 类。但问题是它永远不会创建列表,并且会出现此错误。

Activity 使用的 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)

最佳答案

看起来 Cursor 没有被关闭。我看到你有 c.close(),但可能无法访问它,因为在它之前抛出了另一个异常。而且,该异常被 DatabaseObjectNotClosedException 屏蔽,最终被抛出以屏蔽原始异常。最好将其包装在 finally 子句中,以确保无论是否抛出异常,它始终关闭。

Cursor c = ...
try {
...
} finally {
c.close();
}

如果您这样做,那么它可能会告诉您真正的问题是什么。不幸的是,由于我们没有看到完整的异常堆栈跟踪,因此我们无法看到在您的代码中究竟从何处抛出 DatabaseObjectNotClosedException。我们只能看到它从 android 代码中的什么地方抛出,但我们看不到过去的 SQLiteDirectCursorDriver.query。

关于android - 完成尚未停用或关闭的 Cursor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8888537/

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