gpt4 book ai didi

android - 当适配器注册了观察者时,无法更改此适配器是否具有稳定 ID

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:24:47 26 4
gpt4 key购买 nike

我在我的 android 项目中使用 RecyclerView,它的性能非常糟糕。来自答案here ,我尝试将 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);

关于android - 当适配器注册了观察者时,无法更改此适配器是否具有稳定 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52460141/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com