0

Ich versuche, ein neues in viewpager'sfragment mit this als Beispiel zu öffnen. Aber ich bekomme diesen Fehler von IndexOutOfBoundsException in der Zeile tabLayout.getTabAt(0).setIcon(tabIcons[0]);. Ich habe den Logcate here eingefügt. Kann mir jemand sagen, wie ich das lösen muss, wie ich tablayout mit meinem viewpager brauche, während das Beispiel nicht hatte?App stürzt ab, während neues Fragment in Viewpager geöffnet wird

public class FirstFragment extends Fragment { 
 

 
\t private static final String TAG = "FirstFragment"; 
 

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

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

 
\t \t btn.setOnClickListener(new OnClickListener() { 
 

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

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

 
\t \t \t \t trans.commit(); 
 
\t \t \t } 
 
\t \t }); 
 

 
\t \t return view; 
 
\t } 
 

 
}
First tab: 
 

 
public class RootFragment extends Fragment { 
 

 
\t private static final String TAG = "RootFragment"; 
 

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

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

 
\t \t transaction.commit(); 
 

 
\t \t return view; 
 
\t } 
 

 
}
Main Activity: 
 

 
public class MainActivity extends AppCompatActivity { 
 

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

 
\t SlidePagerAdapter mPagerAdapter; 
 

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

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

 
\t } 
 

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

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

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

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

 
}

Antwort

1

Verwendung unterhalb Funktion

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

Sie haben keine Tab zu Ihrem tabLayout hinzugefügt, so dass Sie IndexOutOfBoundsException

+0

Erstaunlicher Mann bekommen! Das funktioniert wie ein Charme :) – Sammy

Verwandte Themen