打开导航抽屉,点击事件的布局

时间:2015-12-08 07:32:52

标签: java android xml android-layout

我有两个导航抽屉,一个带有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的视图?

2 个答案:

答案 0 :(得分:1)

请使用以下代码打开和关闭抽屉

 public void toggleDrawer() {

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

答案 1 :(得分:1)

rigthMenu OnClickListener中有一些错误。首先,没有必要在那里夸大rigth_menu布局。您已将其包含在主布局中,因此它已经被夸大了。此外,rigth_menu不包含标识为View的任何layRigth,因此text.findViewById(R.id.layRigth)返回null,并导致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);
            }
        }
    });