Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Pager Issue on Pause

Tags:

android

pager

I'm building an application using Pager with the compatibility library. My application forceClose whenever I try to pause it (when I come back to my desktop for instance).

I have never encountered any error of that type and therefore, I've no idea how to solve it.

I'll show you my code whereas i'm quite sure you'll don't have to look at it :

public class *** extends FragmentActivity {
    /** Called when the activity is first created. */

    static final int NUM_ITEMS = 5;
    static final int NUM_ACTUALITE = 0;
    static final int NUM_SELECTION = 1;
    static final int NUM_MULTIMEDIA = 2;
    static final int NUM_FESTIVALIER = 3;
    static final int NUM_***E = 4;

    MyAdapter mAdapter;

    ViewPager mPager;

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

        mAdapter = new MyAdapter(getSupportFragmentManager());

        mPager = (ViewPager) findViewById(R.id.pager);
        mPager.setAdapter(mAdapter);


    }

    public static class MyAdapter extends FragmentPagerAdapter {
        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

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

        @Override
        public Fragment getItem(int position) {

            switch (position) {
            case NUM_ACTUALITE:
                return ActuFragment.newInstance();
            case NUM_SELECTION:
                return SelectionFragment.newInstance();
            case NUM_MULTIMEDIA:
                return MultimediaFragment.newInstance();
            case NUM_FESTIVALIER:
                return FestivalierFragment.newInstance();
            case NUM_***E:
                return ***eFragment.newInstance();
            default:
                return ActuFragment.newInstance();
            }

        }
    }

and my logCat :

02-15 11:48:03.902: E/AndroidRuntime(11601): FATAL EXCEPTION: main
02-15 11:48:03.902: E/AndroidRuntime(11601): java.lang.RuntimeException: Unable to pause activity {com.**.**/com.**.**.***Activity}: java.lang.NullPointerException
02-15 11:48:03.902: E/AndroidRuntime(11601):    at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2354)
02-15 11:48:03.902: E/AndroidRuntime(11601):    at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2311)
02-15 11:48:03.902: E/AndroidRuntime(11601):    at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:2291)
02-15 11:48:03.902: E/AndroidRuntime(11601):    at android.app.ActivityThread.access$1700(ActivityThread.java:117)
02-15 11:48:03.902: E/AndroidRuntime(11601):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:938)
02-15 11:48:03.902: E/AndroidRuntime(11601):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-15 11:48:03.902: E/AndroidRuntime(11601):    at android.os.Looper.loop(Looper.java:130)
02-15 11:48:03.902: E/AndroidRuntime(11601):    at android.app.ActivityThread.main(ActivityThread.java:3683)
02-15 11:48:03.902: E/AndroidRuntime(11601):    at java.lang.reflect.Method.invokeNative(Native Method)
02-15 11:48:03.902: E/AndroidRuntime(11601):    at java.lang.reflect.Method.invoke(Method.java:507)
02-15 11:48:03.902: E/AndroidRuntime(11601):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-15 11:48:03.902: E/AndroidRuntime(11601):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-15 11:48:03.902: E/AndroidRuntime(11601):    at dalvik.system.NativeStart.main(Native Method)
02-15 11:48:03.902: E/AndroidRuntime(11601): Caused by: java.lang.NullPointerException
02-15 11:48:03.902: E/AndroidRuntime(11601):    at android.support.v4.app.FragmentManagerImpl.saveFragmentBasicState(FragmentManager.java:1576)
02-15 11:48:03.902: E/AndroidRuntime(11601):    at android.support.v4.app.FragmentManagerImpl.saveAllState(FragmentManager.java:1617)
02-15 11:48:03.902: E/AndroidRuntime(11601):    at android.support.v4.app.FragmentActivity.onSaveInstanceState(FragmentActivity.java:481)
02-15 11:48:03.902: E/AndroidRuntime(11601):    at android.app.Activity.performSaveInstanceState(Activity.java:1037)
02-15 11:48:03.902: E/AndroidRuntime(11601):    at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1181)
02-15 11:48:03.902: E/AndroidRuntime(11601):    at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2336)
02-15 11:48:03.902: E/AndroidRuntime(11601):    ... 12 more

A fragment of mines :

public class ActuFragment extends Fragment {

    public static ActuFragment newInstance() {
        ActuFragment f = new ActuFragment();

        return f;
    }

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

        View mainView = inflater.inflate(R.layout.actualites_layout,
                container, false);
        return mainView;

    }
}
like image 608
Renaud Favier Avatar asked Feb 15 '12 10:02

Renaud Favier


2 Answers

I've stumbled upon the same problem while using ActionBarSherlock compatibility library. The workaround is that your adapter extends FragmentStatePagerAdapter instead of FragmentPagerAdapter.

like image 136
LukaCiko Avatar answered Sep 27 '22 17:09

LukaCiko


Instead you can follow the work around here that I have posted earlier for an issue related to NPE while using Tab + Fragment + ViewPager + FragmentStatePagerAdapter

I hope same solution will work for you

like image 24
Lalit Kumar Sahoo Avatar answered Sep 27 '22 17:09

Lalit Kumar Sahoo