我有一個水平的RecyclerView,我將其添加爲Listview的標題。但是一旦我開啓我的活動,它就會崩潰。LayoutManager - 將RecyclerView設置爲ListVIew標題時出現空指針異常
這是我在做什麼在我的活動:
headerView = getLayoutInflater().inflate(R.layout.header_layout, null);
CustomViewHolder holder = new CustomViewHolder(headerView, this);
holder.inflateView();
listView.addHeaderView(headerView);
我的觀點持有者構造函數中:
public CustomViewHolder(View itemView, Activity activity) {
this.mActivity = activity;
mainView = itemView;
recyclerView = (RecyclerView) itemView.findViewById(R.id.recyclerView);
LinearLayoutManager layoutManager = new LinearLayoutManager(mActivity);
layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
layoutManager.canScrollHorizontally();
recyclerView.setLayoutManager(layoutManager);
}
而且在我的觀點持有者充氣方法:
adapter = new CustomHorizontalAdapter(mActivity, list);
recyclerView.setAdapter(adapter);
這是我的觀點XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
但只要我打開我的活動,它與以下日誌崩潰 -
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView$LayoutManager.onMeasure(android.support.v7.widget.RecyclerView$Recycler, android.support.v7.widget.RecyclerView$State, int, int)' on a null object reference
at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:1764)
at android.view.View.measure(View.java:17565)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5537)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17565)
at android.widget.ListView.setupChild(ListView.java:1944)
at android.widget.ListView.makeAndAddView(ListView.java:1867)
at android.widget.ListView.fillDown(ListView.java:698)
at android.widget.ListView.fillFromTop(ListView.java:759)
at android.widget.ListView.layoutChildren(ListView.java:1659)
at android.widget.AbsListView.onLayout(AbsListView.java:2151)
at android.view.View.layout(View.java:15689)
at android.view.ViewGroup.layout(ViewGroup.java:5040)
at android.support.v4.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:581)
有人可以幫我明白這是爲什麼崩潰?我將佈局管理器設置爲回收站視圖。有沒有其他的方式來添加一個水平滾動視圖作爲頭到一個列表視圖?這不可能嗎?
'RecyclerViewAdapter'是你的'CustomViewHolder'部分嗎? –