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

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

问题描述

尝试还原片段时,我的android应用崩溃了,我遇到了问题.我没有将任何自定义变量添加到要还原的捆绑软件中,这都是默认设置.我正在使用Fragments和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);
}
}

我收到此堆栈跟踪:

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?

推荐答案

一旦我开始将ViewPager和TabState中的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天全站免登陆