[問題] IndexOutOfBoundsException RecyclerView

看板AndroidDev作者 (莫急莫慌莫害怕)時間10年前 (2015/12/14 16:17), 10年前編輯推噓0(004)
留言4則, 2人參與, 最新討論串1/1
IndexOutOfBoundsException in RecyclerView triggered on runOnUiThread(new Runnable() 錯誤訊息 FATAL EXCEPTION: main java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 8(offset:8).state:9 android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4405) android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4363) android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1961) android.support.v7.widget.GridLayoutManager.layoutChunk(GridLayoutManager.java:458) android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1333) android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:562) android.support.v7.widget.GridLayoutManager.onLayoutChildren(GridLayoutManager.java:171) Code內容大致上 public BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() { @Override public synchronized void onLeScan(final BluetoothDevice device, final int rssi, final byte[] scanRecord) { runOnUiThread(new Runnable() { @Override public void run() { synchronized (this) { String deviceName = device.getName(); if (XXX) { XXX } else { XXX } mRecyclerView_RollCall.postDelayed(new Runnable() { @Override public void run() { mRecyclerViewAdapter.notifyDataSetChanged(); } }, 1000); } } }); } }; 找過的一些Solution https://code.google.com/p/android/issues/detail?id=77846 https://code.google.com/p/android/issues/detail?id=77232 我有找了很多Solution不過沒辦法解決問題,麻煩懂得大大可以給點建議! 很多大部分是,下拉刷新,不過我是需要一直更新RecyclerView上的item, 增加或是刪除item已經有另一個線程用ArrayList做了,一開始都可以正常的使用, 過一下後,就會出現這個Excetion! -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 36.225.167.206 ※ 文章網址: https://www.ptt.cc/bbs/AndroidDev/M.1450081056.A.5E0.html

12/14 17:05, , 1F
可能調整一下notifyDataSetChanged的方式
12/14 17:05, 1F

12/14 17:06, , 2F
另外不知道為何要synchronized那個runnable instance
12/14 17:06, 2F

12/14 17:07, , 3F
getDeviceName跟XXX那段如果跟UI都無關 可以不用post
12/14 17:07, 3F

12/15 07:06, , 4F
有用Log檢查過ArrayList的大小嗎
12/15 07:06, 4F
→感謝K大大跟y大大 是我程式邏輯的問題~我調整了notifyDataSetChange的方式 ※ 編輯: freedom74569 (36.225.167.206), 12/16/2015 10:55:37
文章代碼(AID): #1MRdiWNW (AndroidDev)