IllegalArgumentException:LinearLayoutManager已附加到 RecyclerView
当我尝试为Exception
设置LayoutManager
并且说RecyclerView
时,我得到了这个LinearLayoutManager is already attached
但在我为RecyclerView设置LinearLayoutManager之前,我已经检查过RecyclerView
是否包含LinearLayout
如下图所示,您将看到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);
}
我不知道为什么会这样。如何阻止为LinearLayoutManager
设置新的RecyclerView
?
这是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)
答案 0 :(得分:7)
我找到了解决问题的方法
原因是我的LayoutManager
是一个单例对象(因为我使用Dagger
到Inject
它)。
我将其设置为RecyclerView
,然后View
刷新=> RecyclerView
会刷新,我仍然为LayoutManager
设置RecyclerView
,错误将会抛出
=>解决方案是不要将LayoutManager
创建为单身Object