0

Farklı parçalar arasında Gezinme için DrawerLayout kullanıyorum. Fakat benim uygulamamda araç çubuğu kullanmıyorum, araç çubuğu için her bir parçanın düzeninde yer alan lineer bir düzen oluşturdum ve her parçadan erişim için ana etkinliğimde genel olarak Statik düzenini tanımladım.Ve benim drawerlayout main_activity.xml içinde dosya. benim ana aktivitede Drawerlayout opendrawer NullpointerException

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 
<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/drawerLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <FrameLayout 
     android:id="@+id/mainContent" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

    <!-- The navigation drawer --> 
    <RelativeLayout 
     android:layout_width="300dp" 
     android:layout_height="match_parent" 
     android:id="@+id/drawerPane" 
     android:layout_gravity="start"> 

     <!-- Profile Box --> 

     <RelativeLayout 
      android:id="@+id/profileBox" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="@drawable/background_material2" 
      android:padding="8dp" > 


      <ImageView 
       android:id="@+id/avatar" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:src="@drawable/user2" 
       android:layout_marginTop="60dp" /> 

      <LinearLayout 
       android:layout_width="wrap_content" 
       android:layout_height="42dp" 

       android:layout_marginStart="15dp" 
       android:layout_marginLeft="15dp" 
       android:layout_marginTop="115dp" 
       android:layout_toRightOf="@+id/avatar" 
       android:layout_toEndOf="@+id/avatar" 

       android:orientation="vertical" > 

       <TextView 
        android:id="@+id/userName" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="Sushant Kumar" 
        android:textAllCaps="true" 
        android:textColor="#ffffff" 
        android:textSize="16sp" 
        android:textStyle="bold" /> 

       <TextView 
        android:id="@+id/desc" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginTop="4dp" 

        android:text="8931839445" 
        android:textColor="#fff" 
        android:textSize="12sp" /> 
      </LinearLayout> 
     </RelativeLayout> 

     <!-- List of Actions (pages) --> 
     <ListView 
      android:id="@+id/navList" 
      android:layout_width="300dp" 
      android:layout_height="match_parent" 
      android:layout_below="@+id/profileBox" 
      android:choiceMode="singleChoice" 
      android:background="#ffffff" /> 

    </RelativeLayout> 

</android.support.v4.widget.DrawerLayout> 
<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:id="@+id/alert" 
    android:visibility="gone" 
    android:background="@color/PrimaryColor" 
    > 
    <TextView 
     android:layout_width="match_parent" 
     android:gravity="center" 
     android:layout_height="wrap_content" 
     android:textStyle="bold" 
     android:text="Getting Problems while connecting to Server." 
     /> 
</LinearLayout> 
Böyle başlatmak: Ic_menu simgesinin tıklama İşte

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" android:layout_height="match_parent"> 
<LinearLayout 
    android:layout_width="match_parent" 
    android:id="@+id/action_bar" 
    android:orientation="horizontal" 
    android:background="@color/PrimaryColor" 
    android:layout_height="wrap_content"> 
    <ImageView 
     android:layout_width="50dp" 
     android:padding="10dp" 
     android:layout_height="50dp" 
     android:id="@+id/togglebutton" 
     android:src="@drawable/ic_menu" 
     /> 
    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="fill_parent" 
     android:gravity="center" 
     android:text="ABOUT MOOVO" 
     android:id="@+id/title" 
     android:textAllCaps="true" 
     android:textSize="18sp" 
     android:layout_marginRight="50dp" 
     android:textStyle="bold" 
     android:textColor="#FFFFFF" 
     /> 
</LinearLayout> 

: Burada

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

    // Populate the Navigtion Drawer with options 
    mDrawerPane = (RelativeLayout) findViewById(R.id.drawerPane); 
    mDrawerList = (ListView) findViewById(R.id.navList); 
    DrawerListAdapter adapter = new DrawerListAdapter(this, mNavItems); 
    mDrawerList.setAdapter(adapter); 

benim araç çubuğu xml olduğunu Ben açarım Bu kodu kullanarak farklı parçalarından çekmece bölmesi:

Ic_menu.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      MainActivity.mDrawerLayout.openDrawer(MainActivity.mDrawerPane); 
     } 
    }); 

Bu zaman düzgün çoğu çalışma ama bazen bunu vermek NullPointerException.

Fatal Exception: java.lang.NullPointerException Attempt to invoke virtual method 'void android.support.v4.widget.DrawerLayout.openDrawer(android.view.View)' on a null object reference 

Bu sorun nasıl çözülür, herhangi bir öneri?

+0

Eğer kullanarak mDrawerLayout başlatmak mı? mDrawerLayout = findViewById (R.id. ) –

cevap

0

Ic_menu.setOnClickListener(), MainActivity içinde değilse, o zaman geçerli etkinlikteki xml Ana Etkenliğini şişirebilir ve tüm görünümleri burada başlatabilirsiniz. kodu:

View view = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)) 
       .inflate(R.layout.activity_main, null, false); 
     mDrawerLayout = (DrawerLayout) view.findViewById(R.id.drawerLayout); 

     mDrawerPane = (RelativeLayout) view.findViewById(R.id.drawerPane); 

Şimdi aşağıdaki gibi görünmelidir Ic_menu.setOnClickListener() güncel bilgiler alın:

Ic_menu.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      mDrawerLayout.openDrawer(mDrawerPane); 
     } 
    }); 
+0

hala çalışmıyor, mainContent Framelayout – sasuke

+0

'da hala gelen fragmanlardaki drawerlayout'u çağırarak hala NullPointer alıyorum? –

+0

Hayır, çekmece düzeni tıklama etkinliğinde açılmıyor – sasuke