2017-05-08 82 views
-1

我有一個包含3個滑動選項卡的應用程序。所以我首先創建了mainActivity然後創建了3個片段。NullPointerException TabbedActivity中的DrawerLayout

我想實現我的tabbedactivity一個navigationdrawer,但我得到一個錯誤「空指針異常」當我試圖找回我的DrawerLayout ..

我做的所有的代碼上的主要活動,就像我應該做的,不是嗎?或者我需要在每個片段中創建導航抽屜?

這是我的mainactivity XML文件:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/drawerLayout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:fitsSystemWindows="true" 
tools:context="com.example.dasilvadd.students.OngletCours"> 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 


    <android.support.design.widget.AppBarLayout 
     android:id="@+id/appbar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:paddingTop="@dimen/appbar_padding_top" 
     android:theme="@style/AppTheme.AppBarOverlay"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:layout_scrollFlags="scroll|enterAlways" 
      app:popupTheme="@style/AppTheme.PopupOverlay"> 

     </android.support.v7.widget.Toolbar> 

     <android.support.design.widget.TabLayout 
      android:id="@+id/tabs" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" /> 

    </android.support.design.widget.AppBarLayout> 

    <android.support.v4.view.ViewPager 
     android:id="@+id/container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior" /> 

</RelativeLayout> 

<android.support.design.widget.NavigationView 
    android:id="@+id/menu_navigation" 
    android:layout_width="wrap_content" 
    android:layout_height="200dp" 
    android:layout_gravity="start" 
    android:visibility="invisible" 
    app:menu="@menu/navigation_menu"></android.support.design.widget.NavigationView> 

在我mainActivity:

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


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

    setSupportActionBar(toolbar); 
    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 


    //récuperation des differents types de variables pour le menu de navigation 
    mDrawerLayout = (DrawerLayout)findViewById(R.id.drawerLayout); 
    mToogle = new ActionBarDrawerToggle(this,mDrawerLayout,toolbar,R.string.open,R.string.close); 

    //j'ajoute a mon DrawerLayout et je synchronise 
    mDrawerLayout.addDrawerListener(mToogle); 
    mToogle.syncState(); 

     //je récupère le menu de navigation 
    NavigationView navigation = (NavigationView)findViewById(R.id.menu_navigation); 

    // 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); 

    setTitle("Mes cours"); 

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


    //ce Listener permet de detecter quel item a été choisi dans mon menu 
    navigation.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { 
     @Override 
     public boolean onNavigationItemSelected(MenuItem item) { 

      switch (item.getItemId()) 
      { //si c'est "Mes cours" qui a été choisi 
       case R.id.mes_cours: 
        //redirection vers la page "mes cours" 
        Intent cours = new Intent(OngletCours.this,MesCours.class); 
        startActivity(cours); 
        return true; 
       //actions a faire si "mes branches" a été choisi 
       case R.id.mes_branches: 
        Intent branche = new Intent(OngletCours.this,MesBranches.class); 
        branche.putExtra("ajouter","cours"); 
        startActivity(branche); 
        return true; 
       case R.id.mes_jalons: 
        Intent jalons = new Intent(OngletCours.this,PageiTude.class); 
        startActivity(jalons); 
      } 
      return true; 

     } 
    }); 

@Update - 記錄錯誤

Caused by: java.lang.NullPointerException 
                       at com.example.dasilvadd.students.OngletCours.onCreate(OngletCours.java:61) 
                       at android.app.Activity.performCreate(Activity.java:5372) 
                       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104) 
                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257) 
                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)  
                       at android.app.ActivityThread.access$700(ActivityThread.java:159)  
                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)  
                       at android.os.Handler.dispatchMessage(Handler.java:99)  
                       at android.os.Looper.loop(Looper.java:176)  
                       at android.app.ActivityThread.main(ActivityThread.java:5419)  
                       at java.lang.reflect.Method.invokeNative(Native Method)  
                       at java.lang.reflect.Method.invoke(Method.java:525)  
                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)  
                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)  
                       at dalvik.system.NativeStart.main(Native Method)  

你能解釋一下我的錯誤?我希望這不是一個愚蠢的錯誤......謝謝!

回答

0

你試圖讓錯誤DrawerLayout ID drawerLayout2

2.的引用添加ActionBarDrawerToggle聽衆。

試試這個:

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

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 
    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

    mDrawerLayout = (DrawerLayout)findViewById(R.id.drawerLayout); 

    mToogle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.open, R.string.close){ 

     @Override 
     public void onDrawerOpened(View drawerView) { 
      super.onDrawerOpened(drawerView); 
     } 

     @Override 
     public void onDrawerClosed(View drawerView) { 
      super.onDrawerClosed(drawerView); 
     } 
    }; 

    .............. 
    ..................... 
} 
+0

其實,它說空,當我做這一行,這怎麼可能? – Dom

+0

你還在爲NullPointerException嗎?如果是,然後發佈您的錯誤日誌 – FAT

+0

我更新了我的問題檢查日誌錯誤 – Dom