ViewPager PagerObserver was not registered

Marss picture Marss · Sep 22, 2013 · Viewed 7.4k times · Source

My application closes when i turn off my screen and turn it back on.

Logcat tells me that the cause of this error is pointing to java line 60 in my CourseFragment.class which extends Fragment.

mViewPager.setAdapter(infoTechPageAdapter);

I am implementing a ViewPager in my CourseFragment.class which extends Fragment.

Here's my code:

public class CourseFragment extends Fragment {

public static final String ARG_POSITION_NUMBER = "course_number";

private int position;

public CourseFragment() {
    // Empty constructor required for fragment subclasses
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView;
    position = getArguments().getInt("position");

    String course = getResources().getStringArray(
            R.array.array_navigation_drawer)[position];

    getActivity().setTitle(course);

    rootView = inflater.inflate(R.layout.activity_drawer_fragment,
            container, false);

    return rootView;
}// end onCreateView

@Override
public void onStart() {
    super.onStart();

    ViewPager mViewPager = (ViewPager) getActivity().findViewById(
            R.id.pagerYear);

    Log.i("posit", String.valueOf(position));
    switch (position) {

    case 0:
        InfoTechPageAdapter infoTechPageAdapter = new InfoTechPageAdapter(
                getActivity().getSupportFragmentManager());
        mViewPager.setAdapter(infoTechPageAdapter);
        break;

    case 1:
        ComSciPageAdapter comSciPageAdapter = new ComSciPageAdapter(
                getActivity().getSupportFragmentManager());
        mViewPager.setAdapter(comSciPageAdapter);
        break;

    case 2:
        ProfilePageAdapter profilePageAdapter = new ProfilePageAdapter(
                getActivity().getSupportFragmentManager());
        mViewPager.setAdapter(profilePageAdapter);
        break;
    }

}// end onStart
}

Logcat

09-22 10:54:03.830: E/AndroidRuntime(2917): FATAL EXCEPTION: main
09-22 10:54:03.830: E/AndroidRuntime(2917): java.lang.IllegalStateException: Observer android.support.v4.view.ViewPager$PagerObserver@4239b3e8 was not registered.
09-22 10:54:03.830: E/AndroidRuntime(2917):     at android.database.Observable.unregisterObserver(Observable.java:69)
09-22 10:54:03.830: E/AndroidRuntime(2917):     at android.support.v4.view.PagerAdapter.unregisterDataSetObserver(PagerAdapter.java:294)
09-22 10:54:03.830: E/AndroidRuntime(2917):     at com.usjr.sss.adapter.InfoTechPageAdapter.unregisterDataSetObserver(InfoTechPageAdapter.java:21)
09-22 10:54:03.830: E/AndroidRuntime(2917):     at android.support.v4.view.ViewPager.setAdapter(ViewPager.java:409)
09-22 10:54:03.830: E/AndroidRuntime(2917):     at com.usjr.sss.fragment.CourseFragment.onStart(CourseFragment.java:60)
09-22 10:54:03.830: E/AndroidRuntime(2917):     at android.support.v4.app.Fragment.performStart(Fragment.java:1502)
09-22 10:54:03.830: E/AndroidRuntime(2917):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:957)
09-22 10:54:03.830: E/AndroidRuntime(2917):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
09-22 10:54:03.830: E/AndroidRuntime(2917):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
09-22 10:54:03.830: E/AndroidRuntime(2917):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
09-22 10:54:03.830: E/AndroidRuntime(2917):     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
09-22 10:54:03.830: E/AndroidRuntime(2917):     at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
09-22 10:54:03.830: E/AndroidRuntime(2917):     at android.support.v4.view.ViewPager.setAdapter(ViewPager.java:415)
09-22 10:54:03.830: E/AndroidRuntime(2917):     at com.usjr.sss.fragment.CourseFragment.onStart(CourseFragment.java:60)
09-22 10:54:03.830: E/AndroidRuntime(2917):     at android.support.v4.app.Fragment.performStart(Fragment.java:1502)
09-22 10:54:03.830: E/AndroidRuntime(2917):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:957)
09-22 10:54:03.830: E/AndroidRuntime(2917):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
09-22 10:54:03.830: E/AndroidRuntime(2917):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1086)
09-22 10:54:03.830: E/AndroidRuntime(2917):     at android.support.v4.app.FragmentManagerImpl.dispatchStart(FragmentManager.java:1882)
09-22 10:54:03.830: E/AndroidRuntime(2917):     at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:573)
09-22 10:54:03.830: E/AndroidRuntime(2917):     at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1164)
09-22 10:54:03.830: E/AndroidRuntime(2917):     at android.app.Activity.performStart(Activity.java:5114)
09-22 10:54:03.830: E/AndroidRuntime(2917):     at android.app.Activity.performRestart(Activity.java:5169)
09-22 10:54:03.830: E/AndroidRuntime(2917):     at android.app.ActivityThread.handleSleeping(ActivityThread.java:3408)
09-22 10:54:03.830: E/AndroidRuntime(2917):     at android.app.ActivityThread.access$2700(ActivityThread.java:153)
09-22 10:54:03.830: E/AndroidRuntime(2917):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1426)
09-22 10:54:03.830: E/AndroidRuntime(2917):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-22 10:54:03.830: E/AndroidRuntime(2917):     at android.os.Looper.loop(Looper.java:137)
09-22 10:54:03.830: E/AndroidRuntime(2917):     at android.app.ActivityThread.main(ActivityThread.java:5227)
09-22 10:54:03.830: E/AndroidRuntime(2917):     at java.lang.reflect.Method.invokeNative(Native Method)
09-22 10:54:03.830: E/AndroidRuntime(2917):     at java.lang.reflect.Method.invoke(Method.java:511)
09-22 10:54:03.830: E/AndroidRuntime(2917):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
09-22 10:54:03.830: E/AndroidRuntime(2917):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
09-22 10:54:03.830: E/AndroidRuntime(2917):     at dalvik.system.NativeStart.main(Native Method)

Answer

Guillaume Boudreau picture Guillaume Boudreau · Nov 28, 2013

As pointed out by @Luksprog in a comment, you need to change

getActivity().getSupportFragmentManager()

for

getChildFragmentManager()

Why: getSupportFragmentManager() (and getFragmentManager()) is used to interact with fragments associated with this fragment's activity, which is not what you want.

What you want is to place and manage Fragments inside of this Fragment (using your adapters), which is the description of the getChildFragmentManager() method.

A big thanks to @Luksprog for the pointer; I had the same problem you did, and he had the answer! Just posting his answer in a real answer, for others who might not think to look in the comments for an answer to this issue.