在 viewpager 中打开新片段时应用程序崩溃

app crashes while opening new fragment inside viewpager

我正在尝试使用 this as example. But I'm getting this error of IndexOutOfBoundsException in the line tabLayout.getTabAt(0).setIcon(tabIcons[0]);. I pasted the logcate hereviewpager's fragment 中打开一个新的 fragment。任何人都可以告诉我如何解决这个问题,因为我需要 tablayout 和我的 viewpager 而示例没有?

public class FirstFragment extends Fragment {

 private static final String TAG = "FirstFragment";

 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
   Bundle savedInstanceState) {
  // Inflate the layout for this fragment
  View view = inflater.inflate(R.layout.first_fragment, container, false);

  Button btn = (Button) view.findViewById(R.id.btn);

  btn.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    FragmentTransaction trans = getFragmentManager()
      .beginTransaction();
    /*
     * IMPORTANT: We use the "root frame" defined in
     * "root_fragment.xml" as the reference to replace fragment
     */
    trans.replace(R.id.root_frame, new SecondFragment());

    /*
     * IMPORTANT: The following lines allow us to add the fragment
     * to the stack and return to it later, by pressing back
     */
    trans.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    trans.addToBackStack(null);

    trans.commit();
   }
  });

  return view;
 }

}
First tab:

public class RootFragment extends Fragment {

 private static final String TAG = "RootFragment";

 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
   Bundle savedInstanceState) {
  /* Inflate the layout for this fragment */
  View view = inflater.inflate(R.layout.root_fragment, container, false);

  FragmentTransaction transaction = getFragmentManager()
    .beginTransaction();
  /*
   * When this container fragment is created, we fill it with our first
   * "real" fragment
   */
  transaction.replace(R.id.root_frame, new FirstFragment());

  transaction.commit();

  return view;
 }

}
Main Activity:

public class MainActivity extends AppCompatActivity {

 // For this example, only two pages
 static final int NUM_ITEMS = 2;
 private TabLayout tabLayout;
 private ViewPager mPager;
 private int[] tabIcons = {
   R.drawable.facebook,
   R.drawable.twitter
 };

 SlidePagerAdapter mPagerAdapter;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  /* Instantiate a ViewPager and a PagerAdapter. */
  tabLayout=(TabLayout)findViewById(R.id.tabs);
  mPager = (ViewPager) findViewById(R.id.viewpager);
  mPagerAdapter = new SlidePagerAdapter(getSupportFragmentManager());
  mPager.setAdapter(mPagerAdapter);
  setupTabIcons();

 }

 private void setupTabIcons() {
  tabLayout.getTabAt(0).setIcon(tabIcons[0]);
  tabLayout.getTabAt(1).setIcon(tabIcons[1]);
 }

 /* PagerAdapter class */
 public class SlidePagerAdapter extends FragmentPagerAdapter {
  public SlidePagerAdapter(FragmentManager fm) {
   super(fm);
  }

  @Override
  public Fragment getItem(int position) {
   /*
    * IMPORTANT: This is the point. We create a RootFragment acting as
    * a container for other fragments
    */
   if (position == 0)
    return new RootFragment();
   else
    return new StaticFragment();
  }

  @Override
  public int getCount() {
   return NUM_ITEMS;
  }
 }

}

使用下面的函数

  private void setupTabIcons() {
    tabLayout.addTab(tabLayout.newTab().setIcon(tabIcons[0]));
    tabLayout.addTab(tabLayout.newTab().setIcon(tabIcons[1]));
  }

您还没有在 tabLayout 中添加任何选项卡,因此您得到 IndexOutOfBoundsException