Agniveer Kranti
Agniveer Kranti

Reputation: 117

Application crashes after 3rd fragment?

On tab, I have attached five fragments.Till 3rd fragment navigation is fine but when going to 4th and 5th fragment directly or by swiping, app is getting crashed. This is my main activity.

       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;
    }
}
}

This is my fragment class which is right now same for all the five fragments except the class name).

   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

Upvotes: 0

Views: 46

Answers (1)

Rakshit Nawani
Rakshit Nawani

Reputation: 2604

Case 4 is missing in your adapter. Add the Case 4 instead of 5 then it will work fine.

Upvotes: 1

Related Questions