2017-04-13 11 views
0

アンドロイド・スタジオが新しく、ナビゲーション・ドロワーのメニューをクリックすると、この問題が発生します。アクティビティーを使用すると、正常に動作し、レイアウトが表示されます。しかし、私はフラグメントを使用するとクラッシュします。ナビゲーション・ドロワーにフラグメントを使用できません

HERESに私の主な活動:

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)  

とHERESに私のいないクラス:

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); 
    } 
} 
+0

あなた 'no.class'コード – rafsanahmad007

+0

を投稿することがあります今すぐ与えられた – topher

答えて

0

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

はno.classが活動はです。 ? no.classあなたはMainActivityでフレームレイアウトを追加して、このようにアイテムIDに基づいてフラグメントをrelaceできる活動もしてマニフェストファイルに

を宣言する必要があります

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

ごめんなさい。私のコードの他の部分です。 '//引出し関数'を持つものは、引出し内で可能な限り新しいフラグメントを開きたい部分です – topher

関連する問題