使用 Fragments 和 ViewPager 一段时间后,Android 应用程序崩溃 [英] Android app crashing after a while using Fragments and ViewPager

查看:13
本文介绍了使用 Fragments 和 ViewPager 一段时间后,Android 应用程序崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试恢复我的片段时,我的 android 应用程序崩溃了.我没有将任何自定义变量添加到我要恢复的包中,这都是默认的.我正在使用片段和 ViewPager.请参阅下面的代码片段:

I've got a problem with my android app crashing when trying to restore my fragments. I have not added any custom variables to the bundle that I'm trying to restore, It's all default. I'm using Fragments and ViewPager. See my code snippets below:

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

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

    public int getCurrentItemPosition(Fragment fragment){
        return getItemPosition(fragment);
    }

    @Override
    public Fragment getItem(int position) {
        return ContentFragment.newInstance(position);
    }
}

public class MyActivity extends FragmentActivity {

static final int NUM_ITEMS = 100000;
public int currentSelectedPage;
private int changeToFragmentIndex;
public DateTime midDate;
MyAdapter mAdapter;
ViewPager mPager;

/** Called when the activity is first created. */
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.diary);
    MyApplication application = (MyApplication)getApplication();
    application.dataController.myActivity = this;
    mAdapter = new MyAdapter(getSupportFragmentManager());
    mPager = (ViewPager)findViewById(R.id.pager);
    mPager.setAdapter(mAdapter);
    int newDay = application.daysBetween(DateTime.now(), DateTime.parse(application.day.dateToShortString()));

    this.currentSelectedPage = NUM_ITEMS/2+newDay;
    mPager.setCurrentItem(NUM_ITEMS/2+newDay);
    mPager.setOnPageChangeListener(new SimpleOnPageChangeListener(){
        public void onPageSelected(int position){
            currentSelectedPage = position;
            ContentFragment fragment = (ContentFragment) mAdapter.instantiateItem(mPager, currentSelectedPage);
            fragment.loadData();
        }
    });
}

}

public class ContentFragment extends Fragment {
private View v;
static final int NUM_ITEMS = 100000;

static ContentFragment newInstance(int num) {
    ContentFragment f = new ContentFragment();

    return f;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    if (container == null)
        return null;
    v = inflater.inflate(R.layout.diarycontent, container, false);
    return v;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

}

@Override
public void onSaveInstanceState(Bundle savedInstanceState){
    super.onSaveInstanceState(savedInstanceState);
    setUserVisibleHint(true);
}
}

我收到了这个堆栈跟踪:

I receive this stackstrace:

Caused by: java.lang.NullPointerException
at android.support.v4.app.FragmentManagerImpl.getFragment(FragmentManager.java:519)
at android.support.v4.app.FragmentStatePagerAdapter.restoreState(FragmentStatePagerAdapter.java:1 55)
at android.support.v4.view.ViewPager.onRestoreInstanceState(ViewPager.java:522)

据我了解,这可能是支持包的一个已知问题,一种可能的解决方案是在 onSaveInstanceState 中使用 setUserVisibleHint(true).但这没有帮助.

As I have understood this may be a known problem with the support package and that one possible solution would be to use setUserVisibleHint(true) in onSaveInstanceState. But that didn't help.

有谁知道问题的另一种解决方案或我做错了什么?

Does anyone know another solution to the problem or what I've done wrong?

推荐答案

当我开始在 Tab 中使用 ViewPager 和 FragmentStatePagerAdapter 时,我遇到了同样类型的问题.

I have faced same type of issue once I started using ViewPager with FragmentStatePagerAdapter inside Tab.

然后我在所有与此问题相关的论坛上玩了两天,想知道为什么会出现这个问题以及如何解决它,但没有找到任何符合我要求的完美解决方案.

Then I played with all forums related to this issue and break my head for two days to find out why this issue is occurred and how to resolve it but does not found any perfect solution that fulfills as per my requirement.

为了避免 NPE 崩溃,我得到了解决方案,使用 FragmentPagerAdapter 而不是 FragmentStatePagerAdapter.当我替换 FragmentPagerAdapter 而不是 FragmentStatePagerAdapter 时,不会遇到 NPE 崩溃问题,但同一页面不会自行刷新以进行进一步导航(这是我的要求).

I got the solution as in order to avoid the NPE crash use FragmentPagerAdapter instead of FragmentStatePagerAdapter. When I replace FragmentPagerAdapter instead of FragmentStatePagerAdapter then not faced NPE crash issue but the same page is not refreshed itself for further navigation(which is my requirement).

最后覆盖 FragmentStatePagerAdapter 上的 saveState 方法,一切正常.

Finally override the saveState method on FragmentStatePagerAdapter and everything working fine as expected.

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


我如何轻松重现此问题:
我正在使用更高版本(4.1)的设备并按照以下步骤操作:
1.进入设置->开发者选项.
2.点击不保留活动"选项.


How I reproduce this issue easily :
I am using the higher version(4.1) device and followed the below steps:
1. Go to Settings -> Developer Options.
2. Click the option "Do not keep activities".

现在我开始玩我的应用了.

Now I played with my app.

在每次应用程序由于 android.support.v4.app.FragmentManagerImpl.getFragment(Unknown Source) 处的 NPE 而崩溃时覆盖 saveState() 方法之前.但是在覆盖 saveState() 之后没有注册崩溃.

Before override the saveState() method each time the app is crashing due to the NPE at android.support.v4.app.FragmentManagerImpl.getFragment(Unknown Source). But after override saveState() no crash is registered.

我希望这样做不会有任何问题.

I hope by doing so you will not having any issue.

这篇关于使用 Fragments 和 ViewPager 一段时间后,Android 应用程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆