1. 程式人生 > >Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerViewLayoutManager.onMeasur

Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerViewLayoutManager.onMeasur

RecycleView空指標異常

錯誤程式碼如下:java.lang.NullPointerException: Attempt to invoke virtual method ‘void android.support.v7.widget.RecyclerViewLayoutManager.onMeasure(android.support.v7.widget.RecyclerViewRecycler, android.support.v7.widget.RecyclerView$State, int, int)’ on a null object reference

主要原因是LinearLayoutManager問題。在onCreateView里加載含有RecycleView佈局時,RecycleView立馬就需要需要LinearLayoutManager,如果LinearLayoutManager寫在其他地方如handler裡,載入完佈局是找不到LinearLayoutManager就會報空指標異常。

解決方法:直接把LinearLayoutManager寫在onCreateView方法裡,同時還要將其設定給recycleview控制元件並設定其setOrientation屬性
這裡寫圖片描述