参数异常:LinearLayoutManager已经附加到RecyclerView
当我尝试为我的RecyclerView设置LayoutManager时,出现了这个异常,并且它说“LinearLayoutManager已经附加”
但在设置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);
}
我不知道为什么会发生这种情况。我该如何防止为我的
RecyclerView
设置新的LinearLayoutManager
?以下是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)
RecyclerView
。 - LinhlayoutManager
变量? - Mohammed Atif