IllegalArgumentException:LinearLayoutManager已附加到RecyclerView

时间:2016-11-24 10:36:50

标签: android android-layout android-recyclerview

  

IllegalArgumentException:LinearLayoutManager已附加到   RecyclerView

当我尝试为Exception设置LayoutManager并且说RecyclerView时,我得到了这个LinearLayoutManager is already attached 但在我为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);
}

我不知道为什么会这样。如何阻止为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)

1 个答案:

答案 0 :(得分:7)

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

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