创建片段

时间:2016-10-12 01:01:41

标签: android android-fragments nullpointerexception fragmentmanager

我的问题是,在创建新的Fragment后,当FragmentManager设置为null中的当前项时,其ViewPager有时为NullPointerException。 换句话说,这导致ViewPager setCurrentItem()中的FragmentManager,因为它引用了片段的Fragment fragment = PostsFragment.newInstance( some_string_params ); int position = pager_adapter.addFragment( fragment ); Log.i( "MY_TAG", fragment.getFragmentManager() ); //sometimes null !!!!! view_pager.setCurrentItem( position ); //internal NullPointerException

第一次执行以下代码时,FragmentManager几乎从不为null(但是添加print语句有时会改变该行为),并且由于FragmentManager为null,它总是第二次崩溃。

FragmentPagerAdapter

似乎片段仍然在后台设置,并且它还没有准备好及时使用。我该如何解决这个问题?

修改

这是我的public class PagerAdapter extends FragmentPagerAdapter { private final FragmentManager fragment_manager; private final List<Fragment> fragments = new ArrayList<>(); public PagerAdapter( FragmentManager manager ) { super( manager ); this.fragment_manager = manager; } public int addFragment( Fragment fragment ) { fragments.add( fragment ); notifyDataSetChanged(); return fragments.size() - 1; } public void pop() { int index = getCount() - 1; Fragment fragment = fragments.remove( index ); FragmentTransaction transaction = fragment_manager.beginTransaction(); transaction.remove( fragment ); transaction.commit(); fragment_manager.executePendingTransactions(); //execute the commit synchronously notifyDataSetChanged(); } @Override public int getCount() { return fragments.size(); } @Override public Fragment getItem( int position ) { return fragments.get( position ); } }

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.app.FragmentManagerImpl.performPendingDeferredStart(android.support.v4.app.Fragment)' on a null object reference
at android.support.v4.app.FragmentPagerAdapter.setPrimaryItem(FragmentPagerAdapter.java:130)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1175)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:608)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:570)
at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:551)
at fragments.CategoryActivityFragment.onCategoryClicked(CategoryActivityFragment.java:56)
at adapters.CategoriesAdapter$1.onClick(CategoriesAdapter.java:84)
at android.view.View.performClick(View.java:5207)
at android.view.View$PerformClick.run(View.java:21168)
at android.os.Handler.handleCallback(Handler.java:746)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

堆栈跟踪:

_instr

1 个答案:

答案 0 :(得分:1)

您可能应该让该类为您创建片段,不需要将片段存储在列表中。

private final String[] categories = {"cat1", "cat2", "cat3"};
private final String[] titles = {"category 1", "category 2", "category 3"};
public class PagerAdapter extends FragmentPagerAdapter {

  public PagerAdapter(FragmentManager fm) {
    super(fm);
  }

  @Override
  public int getCount() {
    return categories.length;
  }

  @Override
  public Fragment getItem(int position) {
    return PostsFragment.newInstance(categories[position]);
  }

  @Override
  public CharSequence getPageTitle(int position) {
    return titles[position];
  }
}

然后在切换位置时自动创建片段。