0

The Problem: In my onCreate() on my main activity, I am trying to set the text value of a TextView in my fragment which is displayed by a viewpager. When I try to get my fragment instance, It always returns null.

Solutions I Tried: Almost every solution related to this problem on SO gives me the same result(NPE).

My Code is below as well as some explanation...

Home.java Activity (My Main Activity):

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Show the home layout
        setContentView(R.layout.home);

        // Setup the toolbar
        Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        // Now setup our tab bar
        TabLayout tabLayout = (TabLayout)findViewById(R.id.homeTabs);
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

        // Setup the view pager
        ViewPager viewPager = (ViewPager)findViewById(R.id.viewPager);
        PagerAdapter pagerAdapter = new PagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(pagerAdapter);
        tabLayout.setupWithViewPager(viewPager);

        // Start our service controller
        Intent startServiceController = new Intent(this, ServiceController.class);
        startService(startServiceController);

        Fragment viewFragment = pagerAdapter.getRegisteredFragment(viewPager.getCurrentItem());

        if(viewFragment == null){
            Log.v("Fragment", "NULL");
        }
    }

After setting up the pageAdapter and viewpager, I try to access the fragment thats being shown. The code for my PagerAdapter is below...

PagerAdapter:

public class PagerAdapter extends SmartFragmentStatePagerAdapter {

    public PagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                return new ParkListFragment();
            case 1:
                return new MapFragment();
            default:
                return null;
        }
    }

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

    @Override
    public CharSequence getPageTitle(int position) {
        CharSequence title;

        switch (position){
            case 0:
                title = "Spaces Near You";
                break;
            case 1:
                title = "Map";
                break;
            default:
                title = "N/A";
                break;
        }

        return title;
    }
}

SmartFragmentStatePagerAdapter:

public abstract class SmartFragmentStatePagerAdapter extends FragmentStatePagerAdapter {
    // Sparse array to keep track of registered fragments in memory
    private SparseArray<Fragment> registeredFragments = new SparseArray<>();

    public SmartFragmentStatePagerAdapter(FragmentManager fragmentManager) {
        super(fragmentManager);
    }

    // Register the fragment when the item is instantiated
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        Fragment fragment = (Fragment) super.instantiateItem(container, position);
        registeredFragments.put(position, fragment);
        return fragment;
    }

    // Unregister when the item is inactive
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        registeredFragments.remove(position);
        super.destroyItem(container, position, object);
    }

    // Returns the fragment for the position (if instantiated)
    public Fragment getRegisteredFragment(int position) {
        return registeredFragments.get(position);
    }
}

I really don't understand what the issue is. I have tried many things. What I don't want to use is this:

String tag="android:switcher:" + viewId + ":" + index;

ParkListFragment.java:

public class ParkListFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.park_list_fragment, container, false);
    }
}

ParkList Fragment Layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.5">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_centerInParent="true">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/confidenceText"
                android:textAlignment="center"
                android:textSize="24sp"
                android:textColor="@android:color/primary_text_light"
                android:padding="10dp"/>

        </LinearLayout>

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.5">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_centerInParent="true">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/activityText"
                android:textAlignment="center"
                android:textSize="24sp"
                android:textColor="@android:color/primary_text_light"
                android:padding="10dp"/>

        </LinearLayout>

    </RelativeLayout>

</LinearLayout>

Stack Trace: Pastebin

Moussa Harajli
  • 1,486
  • 5
  • 22
  • 36

1 Answers1

0

So after hours of trial and error, the only thing that worked for me was this SO post:

ViewPager Fragments are not initiated in onCreate

Moussa Harajli
  • 1,486
  • 5
  • 22
  • 36