2016-11-24 100 views
0

拋出:IllegalArgumentException:LinearLayoutManager已經連接到 RecyclerView拋出:IllegalArgumentException:LinearLayoutManager已經連接到RecyclerView

,當我嘗試設置LayoutManagerRecyclerView,它說我得到這個ExceptionLinearLayoutManager 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); 
} 

我不知道爲什麼會這樣。如何防止爲我的RecyclerView設置新的LinearLayoutManager

這裏是logcat的

java.lang.IllegalArgumentException: LayoutManager [email protected] 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

你有多個房車嗎? – Blackbelt

+0

@Blackbelt不,我只有1'RecyclerView' –

+0

分享完整代碼 –

回答

2

我找到我的問題的解決方案。
原因是我的LayoutManager是一個單身物件(因爲我用DaggerInject呢)。 我將其設置爲RecyclerView那麼當View刷新=>的RecyclerView會刷新,我仍然設置此LayoutManagerRecyclerView和錯誤會拋出

=>解決方案是不創建LayoutManager作爲一個單身Object

+0

已解決!謝謝 :) –

相關問題