2017-06-09 121 views
0

我的RecyclerView出現問題,我學會了here該錯誤已修復,我應該確保我在主線程中插入數據並調用適當的通知。我這樣做了,但我仍然得到這個例外。Android RecyclerView:IndexOutOfBoundsException:檢測到不一致

onLoadFinisher方法(我使用裝載機加載更多的數據)

@Override 
public void onLoadFinished(Loader<Cursor> loader, Cursor data) 
{ 
    switch (loader.getId()) 
    { 
     case UP_LOADER_ID: 
      Log.d("Adapter", "Thread: "+Thread.currentThread().getName()); 
      days.put(firstDate, data); 
      notifyItemRangeInserted(0, data.getCount()+1); 
      loadingUp = false; 
      break; 

     case DOWN_LOADER_ID: 
      days.put(lastDate, data); 
      notifyItemRangeInserted(getItemCount(), data.getCount()+1); 
      loadingDown = false; 
      break; 

     default: 
      throw new IllegalArgumentException("Unknown loader id: " + loader.getId()); 
    } 
} 

日誌

I/OpenGLRenderer: Initialized EGL, version 1.4 
D/Adapter: Thread: main 
D/Adapter: Thread: main 
D/Adapter: Thread: main 
D/AndroidRuntime: Shutting down VM 
E/AndroidRuntime: FATAL EXCEPTION: main 
        Process: net.antilopa, PID: 6406 
        java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder{fa4fb40 position=42 id=-1497045600000, oldPos=21, pLpos:21 scrap [attachedScrap] tmpDetached not recyclable(1) no parent} 
         at android.support.v7.widget.RecyclerView$Recycler.validateViewHolderForOffsetPosition(RecyclerView.java:5220) 
         at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5402) 
         at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5363) 
         at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5359) 
         at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2141) 
         at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1525) 
         at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1488) 
         at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:585) 
         at android.support.v7.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:3457) 
         at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3252) 
         at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3767) 
         at android.view.View.layout(View.java:16722) 
         at android.view.ViewGroup.layout(ViewGroup.java:5438) 

回答

0

您可以通過getItemCount()取代getItemCount() - 1,因爲getItemCount()返回計數的數量,但是索引值是getItemCount() - 1。

case DOWN_LOADER_ID: 
     days.put(lastDate, data); 
     notifyItemRangeInserted(data.getCount()-1, data.getCount()+1); 
     loadingDown = false; 
     break; 
+0

謝謝,這是真的,但不幸的是我仍然得到相同的異常 –

+0

@F。 Korf你可以使用data.getCount() - 1然後檢查 –

+0

是的,我嘗試使用getCount() - 1,但我仍然得到相同的異常。或者檢查一下? –