片段重载导致数据丢失

Fragment reload causes data lost

我有一个 activity,其中有 2 个片段用作 tabs.I,我有一些数据通过捆绑传递给片段。问题是我的片段重新加载并且包变成空值,这在我尝试获取数据时导致空指针异常。我尝试调试,一开始我得到了正确的数据,但随后它重新加载了片段,我得到了 error.I 有视图寻呼机,我已经设置了屏幕外页面限制,但我仍然得到错误。

我的 activity :

    long selectedDayValues = getIntent().getLongExtra("Selected day",0);
    Bundle bundle = new Bundle();
    bundle.putLong("Selected day", selectedDayValues);
    Fragment expensesForCurrentDayFragment = new ProfitsForCurrentDay();
    expensesForCurrentDayFragment.setArguments(bundle);
    FragmentTransaction transactionExpenses = getSupportFragmentManager().beginTransaction();
    transactionExpenses.add(R.id.fragment_profits_current_day, expensesForCurrentDayFragment).commit();

    Fragment profitsForCurrentDayFragment = new ExpensesForCurrentDay();
    profitsForCurrentDayFragment.setArguments(bundle);
    FragmentTransaction transactionProfits = getSupportFragmentManager().beginTransaction();
    transactionProfits.add(R.id.fragment_expenses_current_day, profitsForCurrentDayFragment).commit();


    // Set up the action bar.
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
    tabLayout.addTab(tabLayout.newTab().setText("Expenses"));
    tabLayout.addTab(tabLayout.newTab().setText("Profits"));
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

    final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
    final SectionsPagerAdapter adapter = new SectionsPagerAdapter
            (getSupportFragmentManager());
    viewPager.setAdapter(adapter);
    viewPager.setOffscreenPageLimit(2);

我在片段中有这个:

Bundle args = getArguments();
long currentDaySeconds = args.getLong("Selected day", 0);

日志猫:

 E/AndroidRuntime: FATAL EXCEPTION: main
 E/AndroidRuntime: java.lang.NullPointerException
 E/AndroidRuntime:     at .ExpensesForCurrentDay.onCreateView(ExpensesForCurrentDay.java:35)
 E/AndroidRuntime:     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
 E/AndroidRuntime:     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1026)
 E/AndroidRuntime:     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1207)
 E/AndroidRuntime:     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
 E/AndroidRuntime:     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1572)
 E/AndroidRuntime:     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:545)
 E/AndroidRuntime:     at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
 E/AndroidRuntime:     at android.support.v4.view.ViewPager.populate(ViewPager.java:1106)
 E/AndroidRuntime:     at android.support.v4.view.ViewPager.populate(ViewPager.java:952)
 E/AndroidRuntime:     at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1474)
 E/AndroidRuntime:     at android.view.View.measure(View.java:15172)
 E/AndroidRuntime:     at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:617)
 E/AndroidRuntime:     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:399)
 E/AndroidRuntime:     at android.view.View.measure(View.java:15172)
 E/AndroidRuntime:     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
 E/AndroidRuntime:     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
 E/AndroidRuntime:     at android.view.View.measure(View.java:15172)
 E/AndroidRuntime:     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
 E/AndroidRuntime:     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
 E/AndroidRuntime:     at android.support.v7.internal.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:124)
 E/AndroidRuntime:     at android.view.View.measure(View.java:15172)
 E/AndroidRuntime:     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
 E/AndroidRuntime:     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
 E/AndroidRuntime:     at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
 E/AndroidRuntime:     at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
 E/AndroidRuntime:     at android.view.View.measure(View.java:15172)
 E/AndroidRuntime:     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
 E/AndroidRuntime:     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
 E/AndroidRuntime:     at android.view.View.measure(View.java:15172)
 E/AndroidRuntime:     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
 E/AndroidRuntime:     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
 E/AndroidRuntime:     at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
 E/AndroidRuntime:     at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
 E/AndroidRuntime:     at android.view.View.measure(View.java:15172)
 E/AndroidRuntime:     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
 E/AndroidRuntime:     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
 E/AndroidRuntime:     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2148)
 E/AndroidRuntime:     at android.view.View.measure(View.java:15172)
 E/AndroidRuntime:     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1848)
 E/AndroidRuntime:     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1100)
 E/AndroidRuntime:     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1273)
 E/AndroidRuntime:     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
 E/AndroidRuntime:     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4212)
 E/AndroidRuntime:     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
 E/AndroidRuntime:     at android.view.Choreographer.doCallbacks(Choreographer.java:555)
 E/AndroidRuntime:     at android.view.Choreographer.doFrame(Choreographer.java:525)
 E/AndroidRuntime:     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
 E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:615)
 E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:92)
 E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:137)
 E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:4745)
 E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
 E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:511)
 E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
 E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
 E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)

我试图将数据保存在 onSaveInstanceState 中,但重新加载发生在片段保存其状态之前。

将片段的创建移动到 FragmentPagerAdapter,正如您在这个美丽的 link 中看到的那样到文档:http://developer.android.com/reference/android/support/v4/app/FragmentPagerAdapter.html

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

    @Override
    public int getCount() {
        return 2; //your number of fragments
    }

    @Override
    public Fragment getItem(int position) {            
         if (position == 1) {
             Fragment fragment = new ProfitsForCurrentDay();
             fragment.setArguments(bundle);
             return fragment;
         } else {
             Fragment fragment = new ExpensesForCurrentDay();
             fragment.setArguments(bundle);
             return fragment;
         }
    }
}

您还可以将选项卡的创建委托给适配器:

mTabLayout.setTabsFromPagerAdapter(mAdapter);

当处理在创建片段之前已知且未更改的片段静态输入时,我使用静态工厂方法,如本例

http://www.androiddesignpatterns.com/2012/05/using-newinstance-to-instantiate.html

Android studio 还可以帮助您创建带有工厂方法的片段,如果您右键单击您的包 → 新建 → 片段 → 空白 → 包括片段工厂方法