我有一个活动与2片段用作标签。我有一些数据,我通过bundle传递给片段。问题是,我的片段重新加载,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);
日志猫: 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中,您可以在这个美丽的文档链接中看到: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.htmlAndroid studio也可以帮助你用工厂方法创建一个片段,如果你右键单击你的包→新建→fragment→空白→包括片段工厂方法