0

我在我的RecyclerView中得到ConcurrentModificationException,只在API 23 & 26,我怎麼能繞過這個? 這是我RecyclerView設置:RecyclerView錯誤ConcurrentModificationException只有API 23和API 26

private void setUpRecyclerView() { 

    int maxIndex = 0; 
    if (game.size() < 3) { 
     maxIndex = game.size(); 
    } else if (game.size() >= 3) { 
     maxIndex = 3; 
    } 
    recyclerView = findViewById(R.id.home_recycler_view); 
    adapter = new GameCardListMiniAdapter(this, game.subList(0, maxIndex)); 
    recyclerView.setAdapter(adapter); 
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(ActivityHome.this); 
    linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL); 
    recyclerView.setHasFixedSize(true); 
    recyclerView.setLayoutManager(linearLayoutManager); 
    recyclerView.setItemAnimator(new DefaultItemAnimator()); 
} 

我從game操縱數據後,我打電話recyclerView.swapAdapter(adapter, true);並且得到錯誤,但只有在上述提到的API,在API的其餘部分可達API 15一切正常正好。 這是API 23堆棧跟蹤:

E/Surface: getSlotFromBufferLocked: unknown buffer: 0xa0d2a5a0 
D/AndroidRuntime: Shutting down VM 
E/AndroidRuntime: FATAL EXCEPTION: main 
Process: com.example.racu.cardgame, PID: 26900 
java.util.ConcurrentModificationException 
    at java.util.AbstractList$SubAbstractList.size(AbstractList.java:360) 
    at com.example.racu.cardgame.adapters.cardgameListMiniAdapter.getItemCount(cardgameListMiniAdapter.java:68) 
    at android.support.v7.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:3603) 
    at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:3103) 
    at android.view.View.measure(View.java:18788) 
    at android.support.v4.widget.NestedScrollView.measureChildWithMargins(NestedScrollView.java:1446) 
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:194) 
    at android.support.v4.widget.NestedScrollView.onMeasure(NestedScrollView.java:512) 
    at android.view.View.measure(View.java:18788) 
    at android.support.constraint.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:934) 
    at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:973) 
    at android.view.View.measure(View.java:18788) 
    at android.support.constraint.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:934) 
    at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:973) 
    at android.view.View.measure(View.java:18788) 
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) 
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465) 
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:748) 
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:630) 
    at android.view.View.measure(View.java:18788) 
    at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:1060) 
    at android.view.View.measure(View.java:18788) 
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) 
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:194) 
    at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139) 
    at android.view.View.measure(View.java:18788) 
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) 
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465) 
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:748) 
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:630) 
    at android.view.View.measure(View.java:18788) 
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) 
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:194) 
    at android.view.View.measure(View.java:18788) 
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) 
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465) 
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:748) 
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:630) 
    at android.view.View.measure(View.java:18788) 
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) 
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:194) 
    at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2643) 
    at android.view.View.measure(View.java:18788) 
    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2100) 
    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1216) 
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1452) 
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107) 
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013) 
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858) 
    at android.view.Choreographer.doCallbacks(Choreographer.java:670) 
    at android.view.Choreographer.doFrame(Choreographer.java:606) 
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844) 
    at android.os.Handler.handleCallback(Handler.java:739) 
    at android.os.Handler.dispatchMessage(Handler.java:95) 
    at android.os.Looper.loop(Looper.java:148) 
    at android.app.ActivityThread.main(ActivityThread.java:5417) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.Zygot 

這是API 26:

D/EGL_emulation: eglMakeCurrent: 0x9d2e70a0: ver 2 0 (tinfo 0xa8e8b510) 
D/AndroidRuntime: Shutting down VM 
E/AndroidRuntime: FATAL EXCEPTION: main 
Process: com.example.racu.cardgame, PID: 4691 
java.util.ConcurrentModificationException 
    at java.util.ArrayList$SubList.size(ArrayList.java:1057) 
    at com.example.racu.cardgame.adapters.cardgameListMiniAdapter.getItemCount(cardgameListMiniAdapter.java:68) 
    at android.support.v7.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:3603) 
    at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:3103) 
    at android.view.View.measure(View.java:21998) 
    at android.support.v4.widget.NestedScrollView.measureChildWithMargins(NestedScrollView.java:1446) 
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:185) 
    at android.support.v4.widget.NestedScrollView.onMeasure(NestedScrollView.java:512) 
    at android.view.View.measure(View.java:21998) 
    at android.support.constraint.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:934) 
    at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:973) 
    at android.view.View.measure(View.java:21998) 
    at android.support.constraint.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:934) 
    at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:973) 
    at android.view.View.measure(View.java:21998) 
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580) 
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514) 
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:806) 
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:685) 
    at android.view.View.measure(View.java:21998) 
    at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:1060) 
    at android.view.View.measure(View.java:21998) 
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580) 
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:185) 
    at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139) 
    at android.view.View.measure(View.java:21998) 
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580) 
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514) 
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:806) 
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:685) 
    at android.view.View.measure(View.java:21998) 
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580) 
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:185) 
    at android.view.View.measure(View.java:21998) 
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580) 
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514) 
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:806) 
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:685) 
    at android.view.View.measure(View.java:21998) 
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580) 
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:185) 
    at com.android.internal.policy.DecorView.onMeasure(DecorView.java:721) 
    at android.view.View.measure(View.java:21998) 
    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2410) 
    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1498) 
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1751) 
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1386) 
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6733) 
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911) 
    at android.view.Choreographer.doCallbacks(Choreographer.java:723) 
    at android.view.Choreographer.doFrame(Choreographer.java:658) 
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897) 
    at android.os.Handler.handleCallback(Handler.java:789) 
    at android.os.Handler.dispatchMessage(Handler.java:98) 
    at android.os.Looper.loop(Looper.java:164) 
    at android.app.ActivityThread.main(ActivityThread.java:6541) 
    at java.lang.reflect.Method.invoke(Native Method) 
08-14 18:50:44.793 4691-4691/com.example.racu.cardgame E/AndroidRuntime:  at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 

試圖使它工作創造game.SubList外,與for循環,而不是一個SubList,沒有崩潰,但swapAdapter沒有被調用,最奇怪的是,這是在我的手機工作很好,沒有崩潰和預期的行爲。

我真的很喜歡一些燈光。 問候Ne0R @銅

回答

0

我找不到任何回答我的問題,我最終創建了一個HorizontalScrollView並將代碼添加到LinearLayoutHorizontalScrollView之內。

0

嘗試

RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this); 

代替:

LinearLayoutManager linearLayoutManager = new LinearLayoutManager(ActivityHome.this); 

然後做同樣的事情EXT

rc.setHasFixedSize(false);//only use this for fixed ammount 
      rc.setLayoutManager(layoutManager); 
      rc.setAdapter(dataItemAdapter); 
+0

謝謝你的建議,但不起作用,我仍然得到同樣的錯誤。 – Racu

相關問題