简体   繁体   English

IllegalArgumentException:LinearLayoutManager已附加到RecyclerView

[英]IllegalArgumentException: LinearLayoutManager is already attached to a RecyclerView

IllegalArgumentException: LinearLayoutManager is already attached to a RecyclerView IllegalArgumentException:LinearLayoutManager已附加到RecyclerView

I got this Exception when I try to set LayoutManager for my RecyclerView and it say LinearLayoutManager is already attached 当我尝试为我的RecyclerView设置LayoutManager并且它LinearLayoutManager is already attached时,我得到了这个Exception
But before I set LinearLayoutManager for my RecyclerView, I already check if my RecyclerView contain LinearLayout or not 但在我为RecyclerView设置LinearLayoutManager之前,我已经检查过我的RecyclerView包含LinearLayout

Like the image below, you will see RecyclerView.LayoutManager m = null but the Exception still throw 如下图所示,您将看到RecyclerView.LayoutManager m = null但异常仍然抛出

在此输入图像描述

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

I don't know why this happened. 我不知道为什么会这样。 How can I prevent set new LinearLayoutManager for my RecyclerView ? 如何防止为RecyclerView设置新的LinearLayoutManager

Here is the logcat 这是logcat

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)

I find the solution for my problem. 我找到了解决问题的方法。
The reason is my LayoutManager is a singleton object (because I use Dagger to Inject it). 原因是我的LayoutManager是一个单例对象(因为我使用DaggerInject它)。 I set it to a RecyclerView then when the View refresh => the RecyclerView will refresh and I still set this LayoutManager for RecyclerView and the error will throw 我将它设置为RecyclerView然后当View refresh => RecyclerView将刷新并且我仍然为RecyclerView设置此LayoutManager并且错误将抛出

=> The solution is don't create LayoutManager as a singleton Object =>解决方案是不要将LayoutManager创建为单例Object

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “LayoutManager 已附加到 RecyclerView”错误 - “LayoutManager is already attached to a RecyclerView” error 未解决的参考:LinearLayoutManager/RecyclerView - Unresolved reference: LinearLayoutManager/RecyclerView 使用LinearLayoutManager集中化RecyclerView - Centralizing RecyclerView with LinearLayoutManager 使用 LinearLayoutManager 在 RecyclerView 中滚动到顶部 - Scroll to top in RecyclerView with LinearLayoutManager 带有GridLayoutManager的RecyclerView和带有LinearLayoutManager的RecyclerView - RecyclerView with GridLayoutManager inside RecyclerView with LinearLayoutManager xamarin RecyclerView LinearLayoutManager StackFromEnd丢失 - xamarin RecyclerView LinearLayoutManager StackFromEnd missing RecyclerView 将 MaterialToggleButtonGroup 添加到父 LinearLayoutManager - RecyclerView add MaterialToggleButtonGroup to parent LinearLayoutManager ItemToucherHelper 不使用垂直 LinearLayoutManager 在 recyclerView 中滑动 - ItemToucherHelper not swipe in recyclerView with vertical LinearLayoutManager RecyclerView 上 LinearLayoutManager 的 scrollToPositionWithOffset 不起作用 - scrollToPositionWithOffset from LinearLayoutManager on RecyclerView not working android recyclerview 中的 DividerItemDecoration 与 LinearLayoutManager - DividerItemDecoration vs LinearLayoutManager in android recyclerview
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM