制作抽屉导航菜单项单击时打开新活动

时间:2016-11-25 13:49:08

标签: android android-studio

我是Android应用程序开发的新手,正在尝试构建应用程序。我已经成功构建了我的导航抽屉,但是我希望菜单项在点击时打开另一个活动。我已经尝试了各种答案,但我的应用程序似乎每次运行我的应用程序后都会崩溃答案。请问这可能是什么原因。

menu.xml文件

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

<item
    android:id="@+id/my_account"
    android:title="My Account"
    android:icon="@mipmap/ic_person_white_24dp"
    android:onClick="settings"></item>

<item
    android:id="@+id/my_settings"
    android:title="Settings"></item>

<item
    android:id="@+id/Logout"
    android:title="Log Out"
    android:icon="@mipmap/ic_exit_to_app_white_24dp"></item>

</menu>

activity.java

package com.example.orume.export;

import android.content.Intent;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;  
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;


public class WorkActivity extends AppCompatActivity {

private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mToggle;

private Toolbar mToolbar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_work);

    mToolbar = (Toolbar)findViewById(R.id.toolbar);
    setSupportActionBar(mToolbar);

    mDrawerLayout = (DrawerLayout)findViewById(R.id.drawerLayout);
    mToggle = new ActionBarDrawerToggle(this, mDrawerLayout,R.string.open, 
R.string.close);

    mDrawerLayout.addDrawerListener(mToggle);
    mToggle.syncState();

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    final Button bExplore = (Button) findViewById(R.id.bExplore);
    bExplore.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view) {
            Intent exploreIntent = new Intent(WorkActivity.this, 
RegisterActivity.class);
            WorkActivity.this.startActivity(exploreIntent);
        }
    });
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {

    if (mToggle.onOptionsItemSelected(item)){
        return true;
    }
    return super.onOptionsItemSelected(item);

}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_search, menu);

    return true;
}

}

activity.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout     
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:id="@+id/drawerLayout"
android:background="#d8dadc">


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <include layout="@layout/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>


<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp">

</RelativeLayout>

<android.support.design.widget.NavigationView
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    app:menu="@menu/navigation_menu"
    android:layout_gravity="start"
    android:background="@color/colorPrimary"
    app:headerLayout="@layout/navigation_header">

</android.support.design.widget.NavigationView>



<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="60dp">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="#d8dadc">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_alignParentBottom="false"
            android:padding="10dp">

            <Button
                android:layout_width="0dp"
                android:layout_height="70dp"
                android:layout_weight="0.5"
                android:background="@mipmap/bg_tool"
                android:id="@+id/bExplore"
                android:layout_marginRight="5dp"/>
            <Button
                android:layout_width="0dp"
                android:layout_height="70dp"
                android:layout_weight="0.5"
                android:background="@mipmap/bg_tool_2"
                android:id="@+id/bPrice"
                android:layout_marginLeft="5dp"
                android:text="Explore"
                android:textAllCaps="false"
                android:textColor="#191919"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_alignParentBottom="false"
            android:padding="10dp"
            android:layout_marginTop="75dp">

            <Button
                android:layout_width="0dp"
                android:layout_height="70dp"
                android:layout_weight="0.5"
                android:background="@mipmap/bg_tool"
                android:id="@+id/bS"
                android:layout_marginRight="5dp"/>
            <Button
                android:layout_width="0dp"
                android:layout_height="70dp"
                android:layout_weight="0.5"
                android:background="@mipmap/bg_tool_2"
                android:id="@+id/bA"
                android:layout_marginLeft="5dp"
                android:text="Explore"
                android:textAllCaps="false"
                android:textColor="#191919"/>
        </LinearLayout>
    </RelativeLayout>
</ScrollView>
</android.support.v4.widget.DrawerLayout>

1 个答案:

答案 0 :(得分:0)

menu.xml您已添加此行android:onClick="settings"。删除该行或将以下代码添加到MainActivity.java

public boolean settings(MenuItem item) { 
// actions 
 return true;
}