在我的应用程序中,经过足够的点击后,我收到此错误:
06-08 19:47:59.967: ERROR/AndroidRuntime(2429): java.lang.RuntimeException: Unable to pause activity {com.MYAPP.app/com.MYAPP.app.MainActivity}: android.database.StaleDataException: Access closed cursor
Run Code Online (Sandbox Code Playgroud)
我有一个Tab Activity(我的MainActivity),它有一个ListActivity作为每个选项卡的内容.在每个ListActivity的onCreate中,我得到一个光标,表示要在该列表中显示的数据.
每个列表的onListItemClick还会创建另一个活动,因此单击列表中的项目将在新屏幕中显示有关该项目的更多信息.这是不一致的,但是在足够点击这些新活动或从新活动返回ListView后,程序崩溃了.
在寻找我的问题的解决方案时,我偶然发现了registerDataSetObserver,但它似乎并不是完整的答案.我也很难找到它的文档,所以我不确定我是否完全理解它.我有一个自定义ListAdapter,我的ListViews都使用它,并在那里的游标上调用了registerDataSetObservers.
我附加了一个ListActivities和我的自定义ListAdapter类中的相关代码.
ListActivity.我有两个,几乎相同,除了它们都有不同的数据库查询创建不同的游标:
import com.MYAPP.app.listmanager.DeviceListAdapter;
public class AllSensorsActivity extends ListActivity{
private DeviceListAdapter AllList;
private DbManager db;
protected Cursor AllCur;
protected Cursor AllSensors;
private static final String TAG = "AllSensorsActivity";
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
Log.e(TAG, "Calling All onCreate");
db = new DbManager(this);
db.open();
AllCur = db.fetchAllDevices();
startManagingCursor(AllCur);
AllSensors = db.fetchAllSensors();
startManagingCursor(AllSensors);
AllList = new DeviceListAdapter(this, AllCur, AllSensors);
setListAdapter(AllList);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id){
String device_name = (String) ((DeviceListAdapter)getListAdapter()).getItem(position);
String sensor_string = ((DeviceListAdapter)getListAdapter()).getSensors(id);
Intent i = new Intent(this, SensorActivity.class);
Bundle bundle = new Bundle();
bundle.putString("NAME", device_name);
i.putExtras(bundle);
bundle.putString("SENSORS", sensor_string);
i.putExtras(bundle);
this.startActivity(i);
}
Run Code Online (Sandbox Code Playgroud)
自定义ListAdapter:
public class DeviceListAdapter extends BaseAdapter {
private static final String TAG = "DeviceListAdapter";
private Context mContext;
private Cursor mSensors;
private Cursor mDevices;
protected MyDataSetObserver sensors_observer;
protected MyDataSetObserver devices_observer;
public DeviceListAdapter(Context context, Cursor devices, Cursor sensors){
mContext = context;
mDevices = devices;
mSensors = sensors;
sensors_observer = new MyDataSetObserver();
mSensors.registerDataSetObserver(sensors_observer);
devices_observer = new MyDataSetObserver();
mDevices.registerDataSetObserver(devices_observer);
}
// ... more functions and stuff that are not relevant go down here...
}
private class MyDataSetObserver extends DataSetObserver {
public void onChanged(){
Log.e(TAG, "CHANGED CURSOR!");
}
public void onInvalidated(){
Log.e(TAG, "INVALIDATED CURSOR!");
}
}
Run Code Online (Sandbox Code Playgroud)
我应该让MyDataSetObserver捕获异常并继续吗?如果可能的话,我想要一个更强大的解决方案.或者是否有其他方法可以重新安排我的程序,以便不会发生staleDataException(经常)?我相信它正在发生,因为我在onListItemClick中启动了新活动.
我相信当它失效时你想要调用requery()光标.你可能会这样做onInvalidated().
| 归档时间: |
|
| 查看次数: |
8614 次 |
| 最近记录: |