0

異なるフラグメント間でのナビゲーションにDrawerLayoutを使用しています。しかし、私のアプリケーションでは、私はツールバーを使用していませんツールバー、私は、各フラグメントのレイアウトに含まれており、各fragment.And私のdrawerlayout main_activity.xmlにアクセスするために私の主な活動で静的にDrawerレイアウトを定義する線形レイアウトを作成ファイル。私の主な活動で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> 

私はこのように初期化します。ここでは

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

は私のツールバーのxmlです:

Ic_menuアイコンのクリックでここ
<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> 

私はこのコードを使用して、異なる断片から引き出しペイン:時間の

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

この正常に動作し、ほとんどが、時にはこれは、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 

どのようにこの問題を解決するには?あなたのIc_menu.setOnClickListener()はMainActivityにない場合

+0

あなたが使用してmDrawerLayoutを初期化しましたか? mDrawerLayout = findViewById(あなたがdrawerlayoutに与えたもの) –

答えて

0

あなたは、現在の活動にMainactivityのXMLを膨らませることができ、そこにすべてのビューを初期化します。 コード:

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

今、あなたの更新Ic_menu.setOnClickListener()は次のようになります。

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

まだ動作していません。mainContent Framelayoutにあるフラグメントからdrawerlayoutを呼び出しています。 – sasuke

+0

まだNullPointerを取得していますか? –

+0

いいえ、引き出しのレイアウトがクリックイベントで開かない – sasuke

関連する問題