2015-12-08 65 views
1

我有兩個導航抽屜,一個是ListView,另一個是佈局。第一個Navigationdrawer工作正常,但第二個無法通過單擊按鈕打開它。打開導航與單擊事件佈局的抽屜

這裏是在MainActivity XML代碼:

<LinearLayout 
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:fitsSystemWindows="true" 
android:layout_gravity="right" 
android:orientation="vertical"> 

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

    <android.support.v7.widget.Toolbar 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@color/orange" 
     android:id="@+id/toolbar" 
     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
     app:navigationIcon="@drawable/arrow" 
     app:title="MyGames" 
     > 
      <Button 
       android:id="@+id/rigthMenu" 
       android:background="@drawable/compose" 
       android:layout_width="36dp" 
       android:layout_height="36dp" 
       android:layout_marginRight="5dp" 
       android:layout_gravity="right" /> 


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

</RelativeLayout> 

<android.support.v4.widget.DrawerLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/drawer_layout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:scrollIndicators="bottom"> 

<FrameLayout 
    android:id="@+id/content_frame" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

</FrameLayout> 

<ListView 
    android:id="@+id/left_drawer" 
    android:layout_width="300dp" 
    android:layout_height="match_parent" 
    android:layout_gravity="start" 
    android:choiceMode="singleChoice" 
    android:divider="@android:color/transparent" 
    android:dividerHeight="2dp" 
    android:background="#ffffff" 
    android:scrollbars="vertical"/> 

    <include 
     android:id="@+id/layRigth" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_gravity="end" 
     layout="@layout/rigth_menu" /> 

</android.support.v4.widget.DrawerLayout> 

這裏是rigthMenu佈局XML代碼:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="#ffffff" 
android:gravity="left"> 

<CheckBox 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="New CheckBox" 
    android:id="@+id/checkBox2" 
    android:layout_gravity="center_horizontal" /> 

<CheckBox 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="New CheckBox" 
    android:id="@+id/checkBox" 
    android:layout_gravity="center_horizontal" /> 

<CheckBox 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="New CheckBox" 
    android:id="@+id/checkBox4" 
    android:layout_gravity="center" /> 

,這裏是在MainActivity的OnCreate代碼:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    // mDrawerLayout2 = (DrawerLayout)findViewById(R.id.layRigth); 
    android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar); 
    toolbar.setNavigationOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      onIcon = !onIcon; 
      if (onIcon == true) { 
       mDrawerLayout.openDrawer(mDrawerList); 
      } else { 
       mDrawerLayout.closeDrawer(mDrawerList); 
      } 
     } 
    }); 

    Button rigthMenu = (Button)findViewById(R.id.rigthMenu); 
    rigthMenu.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      final LayoutInflater factory = getLayoutInflater(); 
      final View text = factory.inflate(R.layout.rigth_menu, null); 
      mDrawerLayout2 = (DrawerLayout)text.findViewById(R.id.layRigth); 
      mDrawerLayout2.openDrawer(mDrawerLayout2); 
     } 
    }); 

    Drawable x = toolbar.getNavigationIcon(); 
    getDefaultSettings("Image", this); 
    // mNavigationView = (NavigationView) findViewById(R.id.shitstuff) ; 

    mFragmentManager = getSupportFragmentManager(); 
    mFragmentTransaction = mFragmentManager.beginTransaction(); 
    mFragmentTransaction.replace(R.id.content_frame,new TabFragment()).commit(); 

    // Initializing 
    dataList = new ArrayList<DrawerItem>(); 
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 
    mDrawerList = (ListView) findViewById(R.id.left_drawer); 

    // Add Drawer Item to dataList 
    dataList.add(new DrawerItem(R.mipmap.ps4miniatura, GREEN, 1)); 
    dataList.add(new DrawerItem(R.mipmap.ps4miniatura, "PC", 0)); 
    dataList.add(new DrawerItem(0, " ", 2)); 
    adapter = new CustomDrawerAdapter(this, R.layout.custom_drawer_item, adapter, 
      dataList); 

// addItem.giveAdapter(adapter); 
    mDrawerList.setAdapter(adapter); 
    mDrawerList.setChoiceMode(ListView.CHOICE_MODE_SINGLE); 
    mDrawerList.setOnItemClickListener(new DrawerItemClickListener()); 

    if (savedInstanceState == null) { 
     SelectItem(1); 
    } 
} 

logcat的錯誤:

12-08 08:40:52.936 15587-15587/com.tutecentral.navigationdrawer E/AndroidRuntime: FATAL EXCEPTION: main 
                       Process: com.tutecentral.navigationdrawer, PID: 15587 
                       java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.widget.DrawerLayout.openDrawer(android.view.View)' on a null object reference 
                        at com.tutecentral.navigationdrawer.MainActivity$2.onClick(MainActivity.java:75) 
                        at android.view.View.performClick(View.java:4820) 
                        at android.view.View$PerformClick.run(View.java:20157) 
                        at android.os.Handler.handleCallback(Handler.java:815) 
                        at android.os.Handler.dispatchMessage(Handler.java:104) 
                        at android.os.Looper.loop(Looper.java:194) 
                        at android.app.ActivityThread.main(ActivityThread.java:5578) 
                        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:955) 
                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:750) 

我有一個nullPointException。我怎樣才能看到佈局rigthMenu的視圖?

+0

讓我們看看你得到的錯誤 - 完整的堆棧跟蹤。 –

回答

1

中有幾個錯誤rigthMenuOnClickListener。首先,不需要在那裏擴大rigth_menu佈局。你已經將它包含在主佈局中,所以它已經被誇大了。此外,rigth_menu不包含任何View與ID layRigth,所以text.findViewById(R.id.layRigth)返回空,並導致NullPointerException。而且,您已經參考了DrawerLayout - mDrawerLayout - 因此您無需再次找到它。

rigthMenu.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      if(mDrawerLayout.isDrawerOpen(GravityCompat.END)) { 
       mDrawerLayout.closeDrawer(GravityCompat.END); 
      } 
      else { 
       mDrawerLayout.openDrawer(GravityCompat.END); 
      } 
     } 
    }); 
+0

它的工作原理..但我不理解爲什麼.. mDrawerLayout是包含兩個NavigationDrawer的競爭對手的參考。所以代碼「GravityCompact.END」在位於佈局結尾的對象處創建一個引用? –

+0

'DrawerLayout'的'openDrawer()'和'closeDrawer()'方法具有重載屬性而不是'View'的重載。所以,這段代碼只是告訴'DrawerLayout'打開/關閉任何一個抽屜'View'即可。這樣,你不需要獲得對實際抽屜'View'的引用;你只需要知道你想打開/關閉哪一邊。 –

+0

忘記。但是,如果我在導航抽屜中有一個按鈕,那麼當我單擊更改片段佈局中的文本時,我應該怎麼做? –

1

請使用下面的代碼來打開和關上抽屜

public void toggleDrawer() { 

     if (mDrawerLayout.isDrawerOpen(GravityCompat.START)) 
      mDrawerLayout.closeDrawer(GravityCompat.START); 
     else 
      mDrawerLayout.openDrawer(GravityCompat.START); 
    } 
+0

我應該打開第二個抽屜,第一個用工具欄的圖標打開。 –

+0

爲第二個抽屜佈局切換添加另一個方法,並在該方法中將上述方法中的「mDrawerLayout」更改爲「mDrawerLayout2」。 – Hits