java - 单击事件打开带布局的抽屉导航

标签 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 的 View ?

最佳答案

rigthMenuOnClickListener 中存在一些错误。首先,没有必要在那里膨胀 rigth_menu 布局。您已将其包含在主布局中,因此它已经膨胀。此外,rigth_menu 不包含任何 ID 为 layRigthView,因此 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);
            }
        }
    });

关于java - 单击事件打开带布局的抽屉导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34150332/

相关文章:

java - 序列化后,Instanceof 对于同一个类返回 false

java - Android - 类变量未及时分配 setOnItemSelectedListener 的值

android - textview的动态布局 - android

xml - 用 xpath 拆分 xml 值并检查字符串位置

java - 如何在 android 中通过缩略图使图像淋浴变大?

java - 和java中main处的错误

java - 如何查找本地IP地址

java - 为什么 Hybris 提供不安全的 http maven 存储库 URL?为什么我们不能覆盖它?

java - Android,不生成ActionBar

java - 如何在最新的 Android Studio 3.2 中打开 Android Device Monitor