-2

I have an activity with 2 fragments used as tabs.I have some data that i pass to the fragments via bundle. The problem is that my fragments reload and the bundle turns into null which causes null pointer exception when i try to get the data. I tried to debug and on the beginning i get the correct data but then it reloads the fragment and I get the error.I have view pager and i have set the off screen page limit but i still get the error.

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

and i have this in the fragments :

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)

I tried to save the data in onSaveInstanceState but the reload happens before the fragments save its state.

Angel
  • 193
  • 1
  • 3
  • 15

2 Answers2

0

Move the creation of the fragments to the FragmentPagerAdapter as you can see at this beautiful link to the docs: 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;
         }
    }
}

You can also delegate the creation of the tabs to the Adapter with:

mTabLayout.setTabsFromPagerAdapter(mAdapter);
juanmeanwhile
  • 2,594
  • 2
  • 24
  • 26
0

When dealing with static inputs on a fragment that are known before the fragment is created and aren't changed, I use a static factory method like this example

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

Android studio can also help you create a fragment with factory methods if you right click your package → New → Fragment → Blank → Include fragment factory methods

Jon
  • 9,156
  • 9
  • 56
  • 73