无法在导航抽屉中使用碎片

时间:2017-04-13 15:31:36

标签: android android-fragments android-studio-2.3

我是android studio的新手,我有这个问题,当我点击导航抽屉中的菜单时...使用Activity,它只是工作正常,它显示布局。但是当我使用Fragments时它会崩溃。

继承人我的主要活动:

public class MainActivity extends AppCompatActivity{
    private DrawerLayout mDrawerLayout;
    private ActionBarDrawerToggle mToggle;
    private Toolbar mToolbar;



    /**
     * The {@link android.support.v4.view.PagerAdapter} that will provide
     * fragments for each of the sections. We use a
     * {@link FragmentPagerAdapter} derivative, which will keep every
     * loaded fragment in memory. If this becomes too memory intensive, it
     * may be best to switch to a
     * {@link android.support.v4.app.FragmentStatePagerAdapter}.
     */
    private SectionsPagerAdapter mSectionsPagerAdapter;

    /**
     * The {@link ViewPager} that will host the section contents.
     */
    private ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mToolbar=(Toolbar)findViewById(R.id.nav_act);
        setSupportActionBar(mToolbar);
        NavigationView navigationView;


        mDrawerLayout=(DrawerLayout)findViewById(R.id.main_content);
        mToggle= new ActionBarDrawerToggle(this, mDrawerLayout,R.string.open,R.string.close);
        mDrawerLayout.addDrawerListener(mToggle);
        mToggle.syncState();

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        // Create the adapter that will return a fragment for each of the three
        // primary sections of the activity.
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.container);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(mViewPager);

        //drawers functions
        navigationView=(NavigationView)findViewById(R.id.navigation_view);
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()){
                    case R.id.nav_ac:
                        startActivity(new Intent(MainActivity.this,pop.class));
                        mDrawerLayout.closeDrawers();
                        break;
                    case R.id.nav_off:
                        startActivity(new Intent(MainActivity.this,no.class));
                        mDrawerLayout.closeDrawers();
                        break;
                    case R.id.nav_am:
                        startActivity(new Intent(MainActivity.this,am.class));
                        mDrawerLayout.closeDrawers();
                        break;
                    case R.id.nav_ab:
                        startActivity(new Intent(MainActivity.this,ab.class));
                        mDrawerLayout.closeDrawers();
                        break;
                }


                return true;
            }
        });


    }







    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.

        if(mToggle.onOptionsItemSelected(item)){
            return true;}

        return super.onOptionsItemSelected(item);
    }



    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {
        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        private static final String ARG_SECTION_NUMBER = "section_number";

        public PlaceholderFragment() {
        }

        /**
         * Returns a new instance of this fragment for the given section
         * number.
         */
        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;
        }


    }

    /**
     * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
     * one of the sections/tabs/pages.
     */
    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position){
            switch(position)
            {
                case 0:
                    Tab1 tab1 = new Tab1();
                    return tab1;
                case 1:
                    Tab2 tab2 = new Tab2();
                    return tab2;
                case 2:
                    Tab3 tab3 = new Tab3();
                    return tab3;
                case 3:
                    Tab4 tab4= new Tab4();
                    return tab4;

            }
            return null;
        }

        @Override
        public int getCount() {
            // Show 3 total pages.
            return 4;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return "Massage";
                case 1:
                    return "Add Ons";
                case 2:
                    return "Foot&Nail Care";
                case 3:
                    return "Benefits";
            }
            return null;
        }
    }
}

然后是错误:

4-13 23:21:33.081 15239-15239/com.example.jackreynaldo.tab E/AndroidRuntime: FATAL EXCEPTION: main
                                                                              Process: com.example.jackreynaldo.tab, PID: 15239
                                                                              java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.jackreynaldo.tab/com.example.jackreynaldo.tab.no}: java.lang.ClassCastException: com.example.jackreynaldo.tab.no cannot be cast to android.app.Activity
                                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2249)
                                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
                                                                                  at android.app.ActivityThread.access$800(ActivityThread.java:154)
                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                  at android.os.Looper.loop(Looper.java:135)
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:5357)
                                                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                                                  at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:952)
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
                                                                               Caused by: java.lang.ClassCastException: com.example.jackreynaldo.tab.no cannot be cast to android.app.Activity
                                                                                  at android.app.Instrumentation.newActivity(Instrumentation.java:1066)
                                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2239)
                                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413) 
                                                                                  at android.app.ActivityThread.access$800(ActivityThread.java:154) 
                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316) 
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                  at android.os.Looper.loop(Looper.java:135) 
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:5357) 
                                                                                  at java.lang.reflect.Method.invoke(Native Method) 
                                                                                  at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:952) 
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747) 

而且我的没有上课:

public class no extends Fragment {

    public no(){

    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.no, container, false);
    }
}

1 个答案:

答案 0 :(得分:0)

  

startActivity(new Intent(MainActivity.this,no.class));

no.class是活动吗? no.class应该是活动并在清单文件中声明

您可以在MainActivity中添加框架布局,然后根据项目Id重新启动片段,如下所示:

getSupportFragmentManager()
  .beginTransaction()
  .replace(R.id.frame, new no())
  .commit();