android - 当适配器已注册观察者时,无法更改此适配器是否具有稳定的 ID
问题描述
我在我的 android 项目中使用 RecyclerView,它的性能真的很糟糕。从这里的答案,我尝试添加adapter.setHasStableIds(true);
到我的代码中。运行时,出现错误:
java.lang.IllegalStateException: Cannot change whether this adapter has stable IDs while the adapter has registered observers.
我的完整 logCat 如下;
09-22 22:22:23.634 1808-1808/com.revosleap.movielist E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.revosleap.movielist, PID: 1808
java.lang.IllegalStateException: Cannot change whether this adapter has stable IDs while the adapter has registered observers.
at android.support.v7.widget.RecyclerView$Adapter.setHasStableIds(RecyclerView.java:6749)
at com.revosleap.movielist.Utils.UrlUtils.GenreFetcher.getGenre(GenreFetcher.java:48)
at com.revosleap.movielist.MainActivity$2.onItemSelected(MainActivity.java:221)
at android.widget.AdapterView.fireOnSelected(AdapterView.java:1124)
at android.widget.AdapterView.access$200(AdapterView.java:54)
at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:1089)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5942)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
请问我该如何解决这个问题?
解决方案
在将适配器分配给您的 recyclerview 之前,您必须将适配器的 hasStableIds 设置为 true。
YourAdapter adapter = new YourAdapter();
adapter.setHasStableIds(true);
myRecyclerView.setAdapter(adapter);
推荐阅读
- python - 找到两个数组 PLOT1 和 PLOT2 之间的最小距离,并存储该点所在的索引
- reverse-engineering - 这是哪种校验和算法?
- java - JSONobject 在 intellij 中有效,但在从命令行运行时无效
- python - 如何使用 SimpleRNN 构建 keras 角色级别模型
- linux - 当节点平台通过 selenium 网格配置为 LINUX 时,操作系统在 Selenium Grid Hub 控制台上显示为未知
- pyomo - Pyomo:DAE参数估计和模型的逐步模拟
- xml - 使用 XPath 从命名空间 XML 有条件地提取值
- php - 插入命令不起作用并且错误消息拆分 $_SESSION[] 变量
- apache-spark - 从 mesos 代理清除 work_dir 中的数据
- c++ - Windows 拼写检查 API 返回不支持的语言