当适配器已注册观察者时,无法更改此适配器是否具有稳定的 ID [英] Cannot change whether this adapter has stable IDs while the adapter has registered observers

查看:15
本文介绍了当适配器已注册观察者时,无法更改此适配器是否具有稳定的 ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的 android 项目中使用 RecyclerView,它的性能非常糟糕.从答案 here,我尝试添加 adapter.setHasStableIds(true);到我的代码.运行时出现错误:

I am using RecyclerView in my android project and its performance has been really awful. From the answers here, I tried adding adapter.setHasStableIds(true); to my code. When running, I got an error:

java.lang.IllegalStateException: Cannot change whether this adapter has stable IDs while the adapter has registered observers.

我的完整 logCat 如下;

My full logCat is as below;

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)

我该如何解决这个问题?

How can I fix this please?

推荐答案

在将适配器分配给回收视图之前,您必须将适配器的 hasStableIds 设置为 true.

You must set the adapter's hasStableIds to true BEFORE you assign the adapter to your recyclerview.

YourAdapter adapter = new YourAdapter();
adapter.setHasStableIds(true);
myRecyclerView.setAdapter(adapter);

这篇关于当适配器已注册观察者时,无法更改此适配器是否具有稳定的 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆