我有一个水平的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)
有人可以帮我理解为什么会崩溃吗?我将布局管理器设置为recycleler视图。有没有其他方法可以将水平滚动视图添加为列表视图的标题?这不可能吗?
答案 0 :(得分:0)
您能否尝试使用Recyclerview的布局管理器。
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);