gpt4 book ai didi

android - 我得到一个 StaleDataException

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

我有一个类,我想在其中使用编辑文本过滤列表,但是每当编辑文本返回空白时,我都会收到 StaleDataException,因此用户输入数据并将其删除,直到 editText 为空白这就是触发异常的地方。所以这是我的代码。

public class CBFilter extends ListActivity {

EditText Filter;
ListView RecipeNames;
Cursor cursor;
CBListAdapter adapter;
CBDataBaseHelper data;
SQLiteDatabase data2;
TextView RecipeText, RowId;
String[] from = { CBDataBaseHelper.KEY_NAME};
int[] to = { R.id.rowText};
ImageView image;
byte[] dataImage;
BufferedInputStream buf;
public static final String TAG = "Error";

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.main);

//FileInputStream in;//= openFileInput("//STEFAN-PC/Users/stefan/Desktop/Uni Work/Image.jpg");
RecipeNames = (ListView) findViewById(android.R.id.list);
RecipeNames.setTextFilterEnabled(true);
RecipeText = (TextView) findViewById(R.id.recipeText);
Filter = (EditText) findViewById(R.id.search_box);

//adapter = new SimpleCursorAdapter (this, 0, cursor, null, null);
//image = (ImageView) findViewById(R.id.RecipeImage);
data = new CBDataBaseHelper(this);
data.open();
cursor = data.query();
startManagingCursor(cursor);
adapter = new CBListAdapter(this, 0, cursor, from, to);
RecipeNames.setAdapter(adapter);
adapter.notifyDataSetChanged();

Filter.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
try{
CBListAdapter filteradapter = (CBListAdapter)RecipeNames.getAdapter();


filteradapter.setFilterQueryProvider(filterQueryProvider);

//RecipeNames.setAdapter(filteradapter);

filteradapter.getFilter().filter(s);
filteradapter.notifyDataSetChanged();

}catch(Exception e){
Log.e(TAG, "sumthing wrong", e);
e.printStackTrace();
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}

public void afterTextChanged(Editable s) {


}




});
}



public void CreateNew(View view){

Intent myIntent = new Intent(this, CBCreate.class);
startActivity(myIntent);
}

@Override
public void onListItemClick(ListView parent, View v, int position, long id) {
super.onListItemClick(parent, v, position, id);
Intent intent1 = new Intent(this, CBCreate.class);
long rowId = cursor.getLong(cursor.getColumnIndex(CBDataBaseHelper.KEY_ROWID));
String s = String.valueOf(rowId);
intent1.putExtra("SELECTED", s);
startActivity(intent1);
}


public FilterQueryProvider filterQueryProvider = new FilterQueryProvider() {

public Cursor runQuery(CharSequence _constraint) {
// CBDataBaseHelper dh2;
Cursor c = data.findRecipe((String) _constraint);
startManagingCursor(c);
Cursor cur = data.query();
startManagingCursor(cur);
cur.requery();

if (_constraint == null | _constraint.length() == 0) {
return cur;
}

return c;
}

};





}

这就是我的 logCat 中显示的内容。

12-15 01:09:00.557: ERROR/AndroidRuntime(12333): android.database.StaleDataException:  Access closed cursor
12-15 01:09:00.557: ERROR/AndroidRuntime(12333): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:217)
12-15 01:09:00.557: ERROR/AndroidRuntime(12333): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41)
12-15 01:09:00.557: ERROR/AndroidRuntime(12333): at pkg.CookBook.CBListAdapter.getView(CBListAdapter.java:41)
12-15 01:09:00.557: ERROR/AndroidRuntime(12333): at android.widget.AbsListView.obtainView(AbsListView.java:1294)
12-15 01:09:00.557: ERROR/AndroidRuntime(12333): at android.widget.ListView.makeAndAddView(ListView.java:1727)
12-15 01:09:00.557: ERROR/AndroidRuntime(12333): at android.widget.ListView.fillDown(ListView.java:652)
12-15 01:09:00.557: ERROR/AndroidRuntime(12333): at android.widget.ListView.fillFromTop(ListView.java:709)
12-15 01:09:00.557: ERROR/AndroidRuntime(12333): at android.widget.ListView.layoutChildren(ListView.java:1580)
12-15 01:09:00.557: ERROR/AndroidRuntime(12333): at android.widget.AbsListView.onLayout(AbsListView.java:1147)
12-15 01:09:00.557: ERROR/AndroidRuntime(12333): at android.view.View.layout(View.java:7034)
12-15 01:09:00.557: ERROR/AndroidRuntime(12333): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
12-15 01:09:00.557: ERROR/AndroidRuntime(12333): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
12-15 01:09:00.557: ERROR/AndroidRuntime(12333): at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
12-15 01:09:00.557: ERROR/AndroidRuntime(12333): at android.view.View.layout(View.java:7034)
12-15 01:09:00.557: ERROR/AndroidRuntime(12333): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
12-15 01:09:00.557: ERROR/AndroidRuntime(12333): at android.view.View.layout(View.java:7034)
12-15 01:09:00.557: ERROR/AndroidRuntime(12333): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
12-15 01:09:00.557: ERROR/AndroidRuntime(12333): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
12-15 01:09:00.557: ERROR/AndroidRuntime(12333): at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
12-15 01:09:00.557: ERROR/AndroidRuntime(12333): at android.view.View.layout(View.java:7034)
12-15 01:09:00.557: ERROR/AndroidRuntime(12333): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
12-15 01:09:00.557: ERROR/AndroidRuntime(12333): at android.view.View.layout (View.java:7034)
12-15 01:09:00.557: ERROR/AndroidRuntime(12333): at android.view.ViewRoot.performTraversals(ViewRoot.java:1049)
12-15 01:09:00.557: ERROR/AndroidRuntime(12333): at android.view.ViewRoot.handleMessage(ViewRoot.java:1744)
12-15 01:09:00.557: ERROR/AndroidRuntime(12333): at android.os.Handler.dispatchMessage(Handler.java:99)
12-15 01:09:00.557: ERROR/AndroidRuntime(12333): at android.os.Looper.loop(Looper.java:143)
12-15 01:09:00.557: ERROR/AndroidRuntime(12333): at android.app.ActivityThread.main(ActivityThread.java:4914)
12-15 01:09:00.557: ERROR/AndroidRuntime(12333): at java.lang.reflect.Method.invokeNative(Native Method)
12-15 01:09:00.557: ERROR/AndroidRuntime(12333): at java.lang.reflect.Method.invoke(Method.java:521)
12-15 01:09:00.557: ERROR/AndroidRuntime(12333): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
12-15 01:09:00.557: ERROR/AndroidRuntime(12333): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

最佳答案

您正在尝试从已关闭的 Cursor 中检索信息。您必须使用 isClosed 来验证游标是否已关闭。方法。

    Cursor cur = data.query();
if (TextUtils.isEmpty((_constraint )){

if (cur != null && cur .moveToFirst()) {
return cur ;

}
}

cur.close();

关于android - 我得到一个 StaleDataException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8518559/

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