0

I have this activity_category having a tabLayout. Inside Others tab I have a recyclerView which displays items from firebase like blog post example but I do not have Images, it is having only text views. It throws a null point exception even if I am initializing recyclerView inside OnCreate method This is my activity_category

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_content"
tools:context="com.solution.engineering.docket_ereceipt.CategoryActivity">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:layout_weight="1"
        app:title="Categories">

    </android.support.v7.widget.Toolbar>

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimaryDark"

        <android.support.design.widget.TabItem
            android:id="@+id/tabItemHardware"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hardware" />

        <android.support.design.widget.TabItem
        android:id="@+id/tabItemStationary"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Stationary" />

        <android.support.design.widget.TabItem
            android:id="@+id/tabItemOthers"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Others" />

    </android.support.design.widget.TabLayout>
</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</RelativeLayout>

Inside one of the tab item (i.e others) I have the RecyclerView which looks like this

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/relativeOthers">

<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/othersRecycler"
    app:layoutManager="android.support.v7.widget.LinearLayoutManager">

</android.support.v7.widget.RecyclerView>
</RelativeLayout>

Java file of activity_catagory uses switch cases to switch between tabs. This is small piece of code how its calling layouts from switch case

public class SectionsPagerAdapter extends FragmentPagerAdapter {
    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }
    @Override
    public Fragment getItem(int position) {
        switch (position){
            case 0:
                viewOthers tab6 = new viewOthers();
                return tab6;
             default:
                return null;
        }
    }

The java file viewOthers() looks like this

public class viewOthers extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.others_layout, container, false);

    return rootView;
}
}

I initialize recyclerView here in CategoryActivity.java

public class CategoryActivity extends AppCompatActivity {
public SectionsPagerAdapter mSectionsPagerAdapter;
private RecyclerView mReceipt;
private DatabaseReference mRef;
public ViewPager mViewPager;
public int extrasPosition;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_category);

    //NOW HERE IT SHOWS NULL POINT EXCEPTION
    mReceipt = (RecyclerView)findViewById(R.id.othersRecycler);
    mReceipt.setHasFixedSize(true);
    mReceipt.setLayoutManager(new LinearLayoutManager(this));

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    // Create the adapter that will return a fragment for each of the three
    // primary sections of the activity.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(mViewPager);
    tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);

    mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));

    //display the desired fragment
    mViewPager.setCurrentItem(extrasPosition);


}

I don't understand why? Any help is appreciated. Thanks in advance.

2
  • chance of getting error using recyclerview inside fragment is more unless you mange it in correct override methods.
    – Rashiq
    Mar 12, 2018 at 10:58
  • You might be able to see where your null exception comes from in your stack trace - could it be that the viewpager loads page at index 1 (pre loads), and you return null if position is not 0 (which you do in your switch case where you make your fragment)? Mar 12, 2018 at 11:02

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.