我是靠谱客的博主 粗暴眼神,最近开发中收集的这篇文章主要介绍FragmentStatePagerAdapter的getFragment(int position) 导致的NullPointerException,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

totalList.add(TaskFragment.newInstance(TASK_TYPE_DAILY));
        totalList.add(TaskFragment.newInstance(TASK_TYPE_GROW));
        totalList.add(TaskFragment.newInstance(TASK_TYPE_CYCLE));

        viewpagerTask.setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) {
            @Override
            public int getCount() {
                return totalList.size();
            }

            @Override
            public Fragment getItem(int position) {
                return totalList.get(position);
            }

            @Override
            public CharSequence getPageTitle(int position) {
                if (position == 0) {
                    return "日常任务";
                } else if (position == 1) {
                    return "成长任务";
                } else {
                    return "周期任务";
                }
            }
        });
        viewpagerTask.setOffscreenPageLimit(3);

我的TaskActivity中有一个viewpager,装载3个fragment碎片:

发现偶尔有时候会出现以下崩溃log:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sina.book/com.sina.book.ui.activity.bookstore.TaskActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.util.SparseArray.get(int)' on a null object reference
	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3000)
	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3078)
	at android.app.ActivityThread.-wrap11(Unknown Source:0)
	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
	at android.os.Handler.dispatchMessage(Handler.java:106)
	at android.os.Looper.loop(Looper.java:187)
	at android.app.ActivityThread.main(ActivityThread.java:7025)
	at java.lang.reflect.Method.invoke(Native Method)
	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:514)
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:888)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.util.SparseArray.get(int)' on a null object reference
	at android.support.v4.app.FragmentManagerImpl.getFragment(FragmentManager.java:905)
	at android.support.v4.app.FragmentStatePagerAdapter.restoreState(FragmentStatePagerAdapter.java:216)
	at android.support.v4.view.ViewPager.onRestoreInstanceState(ViewPager.java:1455)
	at android.view.View.dispatchRestoreInstanceState(View.java:17993)
	at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3859)
	at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3865)
	at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3865)
	at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3865)
	at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3865)
	at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3865)
	at android.view.View.restoreHierarchyState(View.java:17971)
	at com.android.internal.policy.PhoneWindow.restoreHierarchyState(PhoneWindow.java:2198)
	at android.app.Activity.onRestoreInstanceState(Activity.java:1173)
	at android.app.Activity.performRestoreInstanceState(Activity.java:1128)
	at android.app.Instrumentation.callActivityOnRestoreInstanceState(Instrumentation.java:1260)
	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2973)
	... 9 more
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.util.SparseArray.get(int)' on a null object reference
	at android.support.v4.app.FragmentManagerImpl.getFragment(FragmentManager.java:905)
	at android.support.v4.app.FragmentStatePagerAdapter.restoreState(FragmentStatePagerAdapter.java:216)
	at android.support.v4.view.ViewPager.onRestoreInstanceState(ViewPager.java:1455)
	at android.view.View.dispatchRestoreInstanceState(View.java:17993)
	at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3859)
	at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3865)
	at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3865)
	at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3865)
	at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3865)
	at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3865)
	at android.view.View.restoreHierarchyState(View.java:17971)
	at com.android.internal.policy.PhoneWindow.restoreHierarchyState(PhoneWindow.java:2198)
	at android.app.Activity.onRestoreInstanceState(Activity.java:1173)
	at android.app.Activity.performRestoreInstanceState(Activity.java:1128)
	at android.app.Instrumentation.callActivityOnRestoreInstanceState(Instrumentation.java:1260)
	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2973)
	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3078)
	at android.app.ActivityThread.-wrap11(Unknown Source:0)
	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
	at android.os.Handler.dispatchMessage(Handler.java:106)
	at android.os.Looper.loop(Looper.java:187)
	at android.app.ActivityThread.main(ActivityThread.java:7025)
	at java.lang.reflect.Method.invoke(Native Method)
	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:514)
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:888)

然后这个bug不是必现,所以复现起来有些麻烦。幸好网上大牛还是厉害,找到了复习办法:

我使用更高版本(4.1)设备并按照以下步骤操作:
1.转到设置 – >开发者选项.
2.单击“不要保留活动”选项.

怎么解决呢?

经过一番Google,得到一些解决办法,有两种:

第一种:讲FragmentStatePagerAdapter改为FragmentPagerAdapter;

第二种:重写FragmentStatePagerAdapter的saveState()方法,直接return null;

@Override
public Parcelable saveState() {
    // Do Nothing
    return null;
}

第三种:重写FragmentStatePagerAdapter的restoreState方法,捕获异常



            @Override
            public void restoreState(Parcelable state, ClassLoader loader) {
                try {
                    super.restoreState(state, loader);
                } catch (NullPointerException e) {

                }
            }

其实,第二种方法屏蔽了FragmentStatePagerAdapter的保存数据的特性之后,就跟第一种方法没有区别了!

说改就改,过几天看看测试的妹子会不会找过来!

最后

以上就是粗暴眼神为你收集整理的FragmentStatePagerAdapter的getFragment(int position) 导致的NullPointerException的全部内容,希望文章能够帮你解决FragmentStatePagerAdapter的getFragment(int position) 导致的NullPointerException所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(35)

评论列表共有 0 条评论

立即
投稿
返回
顶部