Android ExpandableListView SimpleCursorTreeAdapter 启动空会崩溃
我正在编写一个 Android 应用程序(针对 Android 2.1 版以上)。我正在使用 ExpandableListView
和 SimpleCursorTreeAdapter
。我发现按照以下顺序它会崩溃:
ExpandableListView
启动为空(即数据库中没有相应的记录)。- 添加记录。
- 返回带有
ExpandableListView
的屏幕。现在,显示组。 - 点击群组展开,活动将被强制关闭。
经过几天的互联网搜索,然后痛苦地追踪我的应用程序的源代码,然后是 Android 的平台源代码,
似乎问题是:当 Android 平台启动 SimpleCursorTreeAdapter
时,
它会在实例化期间将子字段列表存储在私有成员mChildFrom
中一次,并且(奇怪的是)ON CONDITION THAT如果至少有一个组。如果没有记录(因此没有组),则不会存储子字段列表。随后添加记录以及将组扩展到显示的子行时,ExpandableListView
将崩溃,因为私有成员 mChildFrom
为 null。
所以,我现在的解决方法是:仅在数据库中有相关记录时实例化 SimpleCursorTreeAdapter
。如果没有,请在 OnResume()
期间重试实例化。
如果有人有更好的崩溃原因,或者有更好的解决方案,这对我(或其他有同样问题的可怜人)会有帮助。 (我倾向于认为这是Android平台的一个bug。)
I am writing an Android app (targets at Android ver 2.1 up). I am using an ExpandableListView
with a SimpleCursorTreeAdapter
. I find that it would crash follow this sequence:
- The
ExpandableListView
starts up empty (i.e. no corresponding records in the database). - Record(s) are added.
- Return to the screen with the
ExpandableListView
. Now, group(s) are shown. - Click on the group to expand, the activity will be forced close.
After days of Internet searching and then painful tracing of my application's source and then Android's platform source,
it seems that the problem is: when the Android platform initiates Ithe SimpleCursorTreeAdapter
,
it would store the list of children fields in private member mChildFrom
once during instantiation and (strangely) ON CONDITION THAT if there is at least one group. If there is no record (and therefore no group), the list of children fields are not stored. When records are added subsequently and when expanding group to shown children rows, the ExpandableListView
will crash because the private member mChildFrom
is null.
So, my get-around right now is: only instantiate the SimpleCursorTreeAdapter
if there is relevant record in the database. If not, retry the instantiation during OnResume()
.
It would be helpful to me (or other poor guys having the same problem) if anybody has better reason for the crash, or has better solution. (I incline to believe that it is a bug in the Android platform.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试以下操作。在 onCreate() 中实例化适配器,但传递 null 作为游标参数。然后在 onResume() 中创建新的 Cursor 并使用 changeCursor()。这样,每当您更改数据模型上的任何内容(添加、删除、编辑行)时,您总是会得到一个新的光标。
Try the following. Instantiate the adapter in onCreate() but pass null as cursor argument. In onResume() you then create new Cursor and pass it to the adapter using the changeCursor(). By this you always get a new cursor whenever you change anything (add, delete, edit row) on you data model.