[问题] IndexOutOfBoundsException RecyclerView

楼主: freedom74569   2015-12-14 16:17:27
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!
作者: KeySabre (KeySabreur)   2015-12-14 17:05:00
可能调整一下notifyDataSetChanged的方式另外不知道为何要synchronized那个runnable instancegetDeviceName跟XXX那段如果跟UI都无关 可以不用post
作者: y3k (激流を制するは静水)   2015-12-15 07:06:00
有用Log检查过ArrayList的大小吗

Links booklink

Contact Us: admin [ a t ] ucptt.com