我尝试使用this作为示例在fragment
viewpager's
中打开新的fragment
。但我在行IndexOutOfBoundsException
中收到tabLayout.getTabAt(0).setIcon(tabIcons[0]);
的错误。我粘贴了logcate here。任何人都可以告诉我如何解决这个问题,因为我需要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;
}
}
}

答案 0 :(得分:1)
使用以下功能
private void setupTabIcons() {
tabLayout.addTab(tabLayout.newTab().setIcon(tabIcons[0]));
tabLayout.addTab(tabLayout.newTab().setIcon(tabIcons[1]));
}
您尚未向tabLayout
添加任何标签,因此您获得了IndexOutOfBoundsException