1

So I am using two RecyclerViews in a fragment in my android app. One of them scrolls horizontally and the other scrolls vertically. Yet for some strange reason the vertical one always crashes with the following error:

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.support.v7.widget.RecyclerView$LayoutManager.canScrollHorizontally()' on a null object reference

Here's how I set up the 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;
}

The RecyclerViews in fragment_home.xml

<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"/>

As soon as I switch the orientation to Horizontal, the RecyclerView crashes as soon as I scroll it. Any suggestions?

  • 1
    Did you intend to have your `nearbyRecycler` also use `buddiesAdapter` or its own adapter? – akodiakson Dec 14 '15 at 21:08
  • I will eventually use a second adapter for the `nearbyRecycler` but right now I'm simply using the same one to isolate the problem to the orientation of the layout. – Vanshil Shah Dec 14 '15 at 23:01

1 Answers1

0

I figured it out! So I'm completely unsure about why this was an issue, but the root of the problem was that my fragment was inside a view pager. I took it out of the view pager for now and added the fragment through fragment transaction.

My previous method of adding the fragment was this way.

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>

I changed it to this:

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>