Android - 轨迹球、ListView 可见性出现错误
我有一个列表视图,显示用户添加的配置文件列表。如果不存在配置文件,我将列表视图可见性设置为“消失”。
我的列表视图上有一个上下文菜单,允许用户删除配置文件。执行此操作时,我会显示一个对话框窗口以确认他们想要删除配置文件。
如果用户通过触摸按钮在对话框中选择“是”,则一切正常。
如果用户使用轨迹球选择“是”,然后触摸屏幕,应用程序就会崩溃。
我可以通过注释掉将列表视图可见性设置为“消失”的代码行来防止崩溃。我很好奇导致崩溃的原因(想了解)。
该错误似乎误导了我,即: java.lang.IllegalStateException:适配器的内容已更改,但 ListView 未收到通知。确保适配器的内容不会从后台线程修改,而仅从 UI 线程修改
I have a listview that displays a list of profiles added by a user. If no profiles exist, I set the listview visibility to "gone".
I have a context menu on the listview that allows a user to delete a profile. When doing this, I display a dialog window to confirm they want to delete the profile.
If the user selects "Yes" on the dialog by touching the button, everything works fine.
If the user selects "Yes" using the trackball, then touches the screen, the app crashes.
I can prevent the crash by commenting out the line of code that sets the listview visibility to "gone". I'm curious what's causing the crash (want to understand).
The error seems misleading to me, which is:
java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ListView 使用的适配器只需要收到数据更改的通知 (notifyDataSetChanged())。仍然不确定为什么使用轨迹球与触摸之间的行为不同,但我现在对适配器的工作原理有了更好的了解。
The adapter that the ListView was using just needed to be notified that the data changed (notifyDataSetChanged()). Still not sure why the behavior is different between using the trackball vs touch, but I have a better understanding of how the adapter works now.