片段重新加载导致数据丢失

时间:2015-10-23 16:53:46

标签: android android-fragments android-viewpager android-tabs android-bundle

我有一个活动,其中2个片段用作制表符。我有一些数据通过包传递给片段。问题是我的片段重新加载并且bundle变成null,当我尝试获取数据时会导致空指针异常。我试图调试,并在开始时我得到正确的数据,但然后它重新加载片段,我得到错误。我有视图寻呼机,我已设置屏幕外页面限制,但我仍然得到错误。

我的活动:

    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);

log cat:

 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中,但重新加载发生在片段保存其状态之前。

2 个答案:

答案 0 :(得分:0)

将片段的创建移动到FragmentPagerAdapter,您可以在这个美丽的文档链接中看到: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);

答案 1 :(得分:0)

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

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

Android工作室还可以帮助您使用工厂方法创建片段,如果您右键单击您的包→新建→片段→空白→包含片段工厂方法