2

My ViewPager does not show any fragments even though it load the fragment for my "Review section". I used the same code with my "rating section" and it work, even tried redo the ViewPager and adapter but still no progress. By the way i'm doing this within a fragment.

I put a log in the adapter to show whether the fragment is working and also change my Viewpager's background color to check if it's visible or not. Even though the ViewPager is visible but the fragment still does not show.

UPDATE: I found out that the problem caused by the activity i use to replace with the fragments.

Here the code snippet (in my activity which hold both "Rating Section" and "review Section".

SubSectionFragment_Rating subSectionFragment_rating = new SubSectionFragment_Rating();
    manager = getSupportFragmentManager();
    manager.beginTransaction().replace(R.id.ratingSection,subSectionFragment_rating, subSectionFragment_rating.getTag()).commit();

    SubSectionFragment_ReviewAndInstructions subSectionFragment = new SubSectionFragment_ReviewAndInstructions();
    manager = getSupportFragmentManager();
    manager.beginTransaction().replace(R.id.subTab,subSectionFragment, subSectionFragment.getTag()).commit();

ViewPager is visible

Should display this fragment in ViewPager

Review.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"
tools:context="com.hybridelements.openchef.fragment_activities.fragment_subs.SubSectionFragment_ReviewAndInstructions">

<RelativeLayout
    android:id="@+id/main_layout"
    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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <android.support.design.widget.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:elevation="6dp"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/tab_layout"
        android:background="@android:color/holo_green_dark"/>

</RelativeLayout>

Review.java (part of the code)

viewPager = (ViewPager) rootView.findViewById(R.id.pager);
    final SubPagerAdapter_ReviewAndInstructions adapter = new SubPagerAdapter_ReviewAndInstructions(getActivity().getSupportFragmentManager(), tabLayout.getTabCount());
    viewPager.setAdapter(adapter);
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));

SubPagerAdapter_ReviewAndInstructions.java

public SubPagerAdapter_ReviewAndInstructions(FragmentManager fm, int NumOfTabs){
    super(fm);
    this.mNumOfTabs = NumOfTabs;
}

@Override
public Fragment getItem(int position) {
    switch (position){
        case 0:
            ReviewsFragment subTab1 = new ReviewsFragment();
            Log.d("ReviewAndInstruction","ReviewsFragment loaded");
            return subTab1;
        default:
            return null;
    }

}

@Override
public int getCount() {
    return mNumOfTabs;
}
  • Make sure you returned rootview in OnCreateView method of fragment. – Mohamed Mohaideen AH Dec 10 '17 at 12:24
  • i've checked to make sure every OnCreateView method return rootView. Still no fix – Burning Violet Dec 10 '17 at 12:56
  • Try with `getChildFragmentManager()` like this: `final SubPagerAdapter_ReviewAndInstructions adapter = new SubPagerAdapter_ReviewAndInstructions(getChildFragmentManager(), tabLayout.getTabCount());` – Yupi Dec 10 '17 at 13:03
  • 1
    When using getChildFragmentManager() . get this error **java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.util.SparseArray.get(int)' on a null object reference** – Burning Violet Dec 10 '17 at 13:06
  • That is the issue with somewhere else in code. Update the question with logcat and code where exception is thrown. – Yupi Dec 10 '17 at 13:13
  • May be try to remove tools:context="com.hybridelements.openchef.fragment_activities.fragment_subs.SubSectionFragment_ReviewAndInstructions" from your xml file and set .Mainactivity to root element. – Mohamed Mohaideen AH Dec 10 '17 at 13:15
  • The problem is that it works, just that the fragment didn't want to show up, the code where the fragment generated in adapter is working, but somehow it didn't show up with no errors – Burning Violet Dec 10 '17 at 13:23
  • @MohamedMohaideenAH tried it, nothing happens – Burning Violet Dec 10 '17 at 13:28
  • update OnCreateView method of ReviewFragments.Java class in Ques. – Mohamed Mohaideen AH Dec 10 '17 at 13:30
  • what kind of update you ask for in ReviewFragments.Java? because there's nothing inside it, i expect the layout will appear in ViewPager as it did on my 'Rating Section" – Burning Violet Dec 10 '17 at 13:57
  • @Yupi The thing is, there are no any exception thrown, that's what caught me off guard. – Burning Violet Dec 10 '17 at 14:03
  • If you are using nested fragments in this case you have to provide `getChildFragmentManager();` to your pager adaper and your min SDK need to be at least 17. For below SDKs you need to use different approach. After this if you get an exception then you have more errors in code. – Yupi Dec 10 '17 at 14:14

2 Answers2

4

Problem solved thanks to @Yupi for the suggestion to change from getActivity().getSupportFragmentManager() to getChildFragment(). during the problem occurred, when change to getChildFragment() got an error

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.util.SparseArray.get(int)' on a null object reference

To fix this error, i need to change my activity that start the fragment transaction, so here's the solution to the problem for nested fragments.

ViewItem.Java (BEFORE)

FragmentManager manager;

manager = getSupportFragmentManager();

ViewItem.Java (AFTER)

FragmentTransaction manager;

manager = getSupportFragmentManager().beginTransaction();

SubPagerFragment_ReviewAndInstructions.java (BEFORE)

final SubPagerAdapter_ReviewAndInstructions adapter = new SubPagerAdapter_ReviewAndInstructions(getActivity().getSupportFragmentManager(), tabLayout.getTabCount());

SubPagerFragment_ReviewAndInstructions.java (AFTER)

final SubPagerAdapter_ReviewAndInstructions adapter = new SubPagerAdapter_ReviewAndInstructions(getChildFragmentManager(), tabLayout.getTabCount());

Result

Fragment correctly displayed

1

In my case, the childFragmentManager and addToBackStack still not fixed my issue, this solution might work instead. With combination from @burning-violet solution.

  1. childFragmentManager used to construct the ViewPager
  2. Using FragmentStatePageAdapter
  3. override restoreState()
  4. Without using addToBackStack(null) // this optional, if you don't want to add to backstack

The restoreState() try catch will only silence the error (NPE), it must be fixed accordingly.

mochadwi
  • 1,190
  • 9
  • 32
  • 87