所以我在我的 android 应用程序的一个片段中使用了两个 RecyclerViews。其中一个水平滚动,另一个垂直滚动。然而由于某种奇怪的原因,垂直的总是崩溃并出现以下错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.support.v7.widget.RecyclerView$LayoutManager.canScrollHorizontally()' on a null object reference
这是我设置 RecyclerViews 的方法:
@InjectView(R.id.nearby_recycler)
RecyclerView nearbyRecycler;
RecyclerView.LayoutManager nearbyLayoutManager;
@InjectView(R.id.buddies_recycler)
RecyclerView buddiesRecycler;
RecyclerView.LayoutManager buddiesLayoutManager;
public static HomeFragment newInstance(){
return new HomeFragment();
}
public HomeFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View root = inflater.inflate(R.layout.fragment_home, container, false);
ButterKnife.inject(this, root);
nearbyLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
nearbyRecycler.setLayoutManager(nearbyLayoutManager);
nearbyRecycler.setAdapter(buddiesAdapter);
buddiesLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
buddiesRecycler.setLayoutManager(buddiesLayoutManager);
buddiesRecycler.setAdapter(buddiesAdapter);
return root;
}
fragment_home.xml 中的 RecyclerViews
<android.support.v7.widget.RecyclerView
android:id="@+id/nearby_recycler"
android:layout_width="match_parent"
android:layout_height="@dimen/icon_large"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/buddies_recycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
一旦我将方向切换到水平,RecyclerView 就会在我滚动时崩溃。有什么建议么?