Linh
Linh

Reputation: 60923

IllegalArgumentException: LinearLayoutManager is already attached to a RecyclerView

IllegalArgumentException: LinearLayoutManager is already attached to a RecyclerView

I got this Exception when I try to set LayoutManager for my RecyclerView and it say LinearLayoutManager is already attached
But before I set LinearLayoutManager for my RecyclerView, I already check if my RecyclerView contain LinearLayout or not

Like the image below, you will see RecyclerView.LayoutManager m = null but the Exception still throw

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);
}

I don't know why this happened. How can I prevent set new LinearLayoutManager for my RecyclerView?

Here is the 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)

Upvotes: 4

Views: 4406

Answers (1)

Linh
Linh

Reputation: 60923

I find the solution for my problem.
The reason is my LayoutManager is a singleton object (because I use Dagger to Inject it). 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

=> The solution is don't create LayoutManager as a singleton Object

Upvotes: 13

Related Questions