第三个片段后应用程序崩溃了吗?

时间:2016-04-25 10:29:47

标签: android android-fragments

在选项卡上,我附加了五个片段。第三个片段导航很好但是当直接转到第4个和第5个片段或通过滑动时,应用程序正在崩溃。 这是我的主要活动。

       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

1 个答案:

答案 0 :(得分:1)

适配器中缺少案例4。添加案例4而不是5然后它将正常工作。