2016-04-25 151 views
0

在選項卡上,我附加了五個片段。第3個片段導航很好,但當直接進入第4個和第5個片段或通過滑動時,應用程序正在崩潰。 這是我的主要活動。應用程序在第3個碎片後崩潰?

 public class MainActivity extends AppCompatActivity { 


private SectionsPagerAdapter mSectionsPagerAdapter; 


private ViewPager mViewPager; 

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

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout); 
    tabLayout.addTab(tabLayout.newTab().setText("Videos")); 
    tabLayout.addTab(tabLayout.newTab().setText("Games")); 
    tabLayout.addTab(tabLayout.newTab().setText("Maps")); 
    tabLayout.addTab(tabLayout.newTab().setText("Quizze")); 
    tabLayout.addTab(tabLayout.newTab().setText("Discussion")); 

    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount()); 


    mViewPager = (ViewPager) findViewById(R.id.container); 
    mViewPager.setAdapter(mSectionsPagerAdapter); 
    mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout)); 
    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { 
     @Override 
     public void onTabSelected(TabLayout.Tab tab) { 
      mViewPager.setCurrentItem(tab.getPosition()); 
     } 

     @Override 
     public void onTabUnselected(TabLayout.Tab tab) { 

     } 

     @Override 
     public void onTabReselected(TabLayout.Tab tab) { 

     } 
    }); 

} 


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 

    int id = item.getItemId(); 


    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 


public static class PlaceholderFragment extends Fragment { 

    private static final String ARG_SECTION_NUMBER = "section_number"; 

    public PlaceholderFragment() { 
    } 


    public static PlaceholderFragment newInstance(int sectionNumber) { 
     PlaceholderFragment fragment = new PlaceholderFragment(); 
     Bundle args = new Bundle(); 
     args.putInt(ARG_SECTION_NUMBER, sectionNumber); 
     fragment.setArguments(args); 
     return fragment; 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment_main, container, false); 
     TextView textView = (TextView) rootView.findViewById(R.id.section_label); 
     textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER))); 
     return rootView; 
    } 
} 

public class SectionsPagerAdapter extends FragmentPagerAdapter { 

    public SectionsPagerAdapter(FragmentManager fm, int tabCount) { 
     super(fm); 
    } 

    @Override 
    public Fragment getItem(int position) { 

     switch (position) { 
      case 0: 
       Videos tab1 = new Videos(); 
       return tab1; 
      case 1: 
       Notes tab2 = new Notes(); 
       return tab2; 
      case 2: 
       MindMaps tab3 = new MindMaps(); 
       return tab3; 


      case 3: 
       Quizze tab4 = new Quizze(); 
       return tab4; 

      case 5: 
       Discussion tab5 = new Discussion(); 
       return tab5; 


     } 
     return null; 
    } 



    @Override 
    public int getCount() { 

     return 5; 
    } 

    @Override 
    public CharSequence getPageTitle(int position) { 
     switch (position) { 
      case 0: 
       return "SECTION 1"; 
      case 1: 
       return "SECTION 2"; 
      case 2: 
       return "SECTION 3"; 
      case 3: 
       return "SECTION 4"; 
      case 4: 
       return "SECTION 5"; 
     } 
     return null; 
    } 
} 
} 

這是我的片段類,現在對於除了類名以外的所有五個片段都是相同的)。

public class Videos extends android.support.v4.app.Fragment { 


     public static Videos newInstance() { 
    Videos fragment = new Videos(); 
    return fragment; 
    } 
    public Videos() { 
    } 
    Button ClickMe; 
    TextView tv; 



     @Override 
      public View onCreateView(LayoutInflater inflater, ViewGroup   container, 
        Bundle savedInstanceState) { 
    View rootView = inflater.inflate(R.layout.video, container, false); 
    ClickMe = (Button) rootView.findViewById(R.id.button); 
    tv = (TextView) rootView.findViewById(R.id.textView2); 



    ClickMe.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View view) { 
    if(tv.getText().toString().contains("Hello")){ 
    tv.setText("Hi molu"); 
    }else tv.setText("Hello"); 
    } 
    }); 
    return rootView; 
    } 
    } // This is the end of our MyFragments Class 
+2

我認爲您的適配器中缺少案例4。添加案例4,那麼它會正常工作 –

+1

更改案例5到案例4 –

+0

您可以發佈logcat嗎? – Raghavendra

回答

1

您的適配器中缺少第4種情況。添加案例4而不是5,那麼它會正常工作。

+0

好吧,等待5分鐘.. @ Rakshit Nawani –

+0

@AgniveerKranti:很高興幫助乾杯! –