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属性
Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerViewLayoutManager.onMeasur_第1张图片

你可能感兴趣的:(android)