Android Pager Issue on Pause
Asked Answered
C

3

5

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;

    }
}
Caucasian answered 15/2, 2012 at 10:57 Comment(5)
must be something wrong with how you're using fragments. have you tried doing it without fragments just yet?Panto
yes, and it seemed to work just fine. do you wan't to see others part of my code ?Caucasian
I've miss read your answer, no I haven't tried it without fragments, actualy I was sure it wasn't possible. but I need them anyway. i've added one of my fragments, they are all lookalikeCaucasian
My guess is that one of your fragments has problems in the save instance step. Try to remove all but the default fragment and see if the error still occurs. If not add them one at a time to see which fragments causes the problem.Ireland
See this link #8748564Depurative
C
26

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

Christcrossrow answered 13/10, 2012 at 11:6 Comment(2)
Works, thanks! Problems started after adding WebView in one fragment.Cooe
after changing to FragmentStatePagerAdapter, its worked for me. Thank u.Chairwoman
B
1

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

Bukovina answered 16/3, 2013 at 3:5 Comment(0)
O
-1

try out mu answer here basically override the onSaveInstanceState, put atleast one value in the bundle. Do for all fragments

Orient answered 25/5, 2013 at 22:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.