[英]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
是一个单例对象(因为我使用Dagger
来Inject
它)。 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.