本文介绍了暂停时出现Android Pager问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用具有兼容性库的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();
            }

        }
    }

和我的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

地雷的一部分:

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;

    }
}

推荐答案

在使用ActionBarSherlock兼容性库时,我偶然发现了相同的问题.解决方法是您的适配器扩展 FragmentStatePagerAdapter 而不是 FragmentPagerAdapter .

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

这篇关于暂停时出现Android Pager问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 03:57