gpt4 book ai didi

android - 非法参数异常 : LinearLayoutManager is already attached to a RecyclerView

转载 作者:太空狗 更新时间:2023-10-29 15:50:46 24 4
gpt4 key购买 nike

IllegalArgumentException: LinearLayoutManager is already attached to a RecyclerView

当我尝试为我的 RecyclerView 设置 LayoutManager 并且它说 LinearLayoutManager 已经附加时,我得到了这个Exception >
但是在我为 RecyclerView 设置 LinearLayoutManager 之前,我已经检查了我的 RecyclerView 是否包含 LinearLayout

如下图,你会看到RecyclerView.LayoutManager m = null 但异常仍然抛出

enter image description here

RecyclerView.LayoutManager m = recyclerView.getLayoutManager();
if(recyclerView.getLayoutManager() != null){
return;
}
try {
recyclerView.setLayoutManager(layoutManager);
}catch (Exception e){
Log.e("AA", "setRecyclerViewLayoutManager: ", e);
}

我不知道为什么会这样。如何防止为我的 RecyclerView 设置新的 LinearLayoutManager

这是日志

java.lang.IllegalArgumentException: LayoutManager android.support.v7.widget.LinearLayoutManager@116d9b78 is already attached to a RecyclerView: android.support.v7.widget.RecyclerView{1cbedba4 VFED.... .......D 0,0-1080,1181 #7f0d00b6 app:id/recycler_news}
at android.support.v7.widget.RecyclerView.setLayoutManager(RecyclerView.java:1087)
at com.toong.map.utils.BindingUtils.setRecyclerViewLayoutManager(BindingUtils.java:76)
at com.toong.map.databinding.FragmentNewsBaseBinding.executeBindings(FragmentNewsBaseBinding.java:191)
at android.databinding.ViewDataBinding.executePendingBindings(ViewDataBinding.java:355)
at android.databinding.ViewDataBinding$6.run(ViewDataBinding.java:172)
at android.databinding.ViewDataBinding$7.doFrame(ViewDataBinding.java:238)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:765)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:549)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)

最佳答案

我找到了我的问题的解决方案。
原因是我的 LayoutManager 是一个单例对象(因为我使用 DaggerInject 它)。我将它设置为 RecyclerView 然后当 View 刷新时 => RecyclerView 将刷新并且我仍然设置此 LayoutManager 对于 RecyclerView 会抛出错误

=> 解决方案是不要将 LayoutManager 创建为单例 Object

关于android - 非法参数异常 : LinearLayoutManager is already attached to a RecyclerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40784158/

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