Vertical LinearLayoutManager崩溃应用程序

时间:2015-12-14 21:01:16

标签: android android-recyclerview linearlayoutmanager

所以我在我的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会在滚动后立即崩溃。有什么建议吗?

1 个答案:

答案 0 :(得分:0)

我明白了!所以我完全不确定为什么这是一个问题,但问题的根源是我的片段在一个视图寻呼机内。我现在把它从视图分页器中取出并通过片段事务添加片段。

我之前添加片段的方法是这样的。

<强> MainActivity.java

HomeFragment homeFragment;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ButterKnife.inject(this);

    homeFragment = HomeFragment.newInstance();
    mFragmentPagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
        @Override
        public Fragment getItem(int position) {
            return homeFragment;
        }

        @Override
        public int getCount() {
            return 1;
        }
    };
}

<强> activity_main.xml中

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    tools:context=".ui.MainActivity">

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_below="@id/toolbar1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</FrameLayout>

我把它更改为:

<强> MainActivity.java

HomeFragment homeFragment;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ButterKnife.inject(this);
    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, homeFragment)
                .commit();
    }
}

<强> activity_main.xml中

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    tools:context=".ui.MainActivity">

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</FrameLayout>