导航菜单不适用于其他活动 - Android Studio

发布于 2025-01-20 17:50:47 字数 2409 浏览 2 评论 0原文

我正在为我的应用程序编写导航菜单。该应用程序从登录屏幕开始,然后进入主要活动。从那里,可以在左侧滑动打开导航栏,然后您将进入任何页面。菜单将让我从主要活动转到一页,但在该页面上我可以通过单击主页返回主要活动,但无法转到任何其他活动。这是我第一次在 java 和 android studio 中编程,所以我正在努力学习我能学到的一切。我尝试在主要活动中使用相同的部分,只是更改意图,因此对于使用 newintent(personal.this,vehicle.class) 或其他内容的个人页面,它不会工作,但如果我做有效的新意图(personal.this, mainactivity.class)

导航抽屉:

  <?xml version="1.0" encoding="utf-8"?>
 <androidx.drawerlayout.widget.DrawerLayout 
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 xmlns:tools="http://schemas.android.com/tools"
 android:id="@+id/drawer_layout"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:fitsSystemWindows="true"
 tools:openDrawer="start">
<include
    android:id="@+id/app_bar_drawer"
    layout="@layout/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<com.google.android.material.navigation.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:layout_marginTop="50dp"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_drawer"
    app:menu="@menu/activity_main_drawer" />
 </androidx.drawerlayout.widget.DrawerLayout>```

MainActivity.java

        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                if(item.getItemId() == R.id.nav_account)
                {
                    startActivity(new Intent(MainActivity.this, account.class));
                    Intent intent = getIntent();
                }

                if(item.getItemId() == R.id.nav_logout)
                {
                    startActivity(new Intent(MainActivity.this, login.class));
                    Intent intent = getIntent();
                }

                if(item.getItemId() == R.id.nav_personal)
                {
                    startActivity(new Intent(MainActivity.this, personal.class));
                    Intent intent = getIntent();
                }```

[![Nav menu][1]][1]


  [1]: https://i.sstatic.net/mzomy.png

I am programming a navigation menu for my app. The app starts with a login screen then moves into the main activity. From there, a navigation bar can be swiped open on the left and you are taken to any of the pages. The menu will let me go from main activity to one page, but then on that page I can go back to main activity by clicking home, but cannot go to any other activities. This is my first time programming in java and android studio so I am trying to learn all I can. I tried using this same section in main activity and just changing the intent, so for the personal page using new intent(personal.this, vehicle.class) or whatever and it won't work but if I do new intent(personal.this, mainactivity.class) that does work.

Nav drawer:

  <?xml version="1.0" encoding="utf-8"?>
 <androidx.drawerlayout.widget.DrawerLayout 
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 xmlns:tools="http://schemas.android.com/tools"
 android:id="@+id/drawer_layout"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:fitsSystemWindows="true"
 tools:openDrawer="start">
<include
    android:id="@+id/app_bar_drawer"
    layout="@layout/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<com.google.android.material.navigation.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:layout_marginTop="50dp"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_drawer"
    app:menu="@menu/activity_main_drawer" />
 </androidx.drawerlayout.widget.DrawerLayout>```

MainActivity.java

        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                if(item.getItemId() == R.id.nav_account)
                {
                    startActivity(new Intent(MainActivity.this, account.class));
                    Intent intent = getIntent();
                }

                if(item.getItemId() == R.id.nav_logout)
                {
                    startActivity(new Intent(MainActivity.this, login.class));
                    Intent intent = getIntent();
                }

                if(item.getItemId() == R.id.nav_personal)
                {
                    startActivity(new Intent(MainActivity.this, personal.class));
                    Intent intent = getIntent();
                }```

[![Nav menu][1]][1]


  [1]: https://i.sstatic.net/mzomy.png

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

看轻我的陪伴 2025-01-27 17:50:47

为什么您添加意图= getIntent()在开始时。 getIntent()用于发送字符串,int,bool从一个活动到第二活动。

                    Intent intent = getIntent();

如果您只想从第一个活动到第二个活动,只需删除此行即可。请参阅下面的新代码。

navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                if(item.getItemId() == R.id.nav_account)
                {
                    startActivity(new Intent(MainActivity.this, account.class));
                }

                if(item.getItemId() == R.id.nav_logout)
                {
                    startActivity(new Intent(MainActivity.this, login.class));
                }

                if(item.getItemId() == R.id.nav_personal)
                {
                    startActivity(new Intent(MainActivity.this, personal.class));
                }

如果您遇到同样的问题。通过评论让我知道

Why you added Intent intent = getIntent() after startActivity. getIntent() works for sending Strings, int, bool from one Activity to Second Activity.

                    Intent intent = getIntent();

Just remove this line if you just want to go from the first to the second activity. See your new code below.

navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                if(item.getItemId() == R.id.nav_account)
                {
                    startActivity(new Intent(MainActivity.this, account.class));
                }

                if(item.getItemId() == R.id.nav_logout)
                {
                    startActivity(new Intent(MainActivity.this, login.class));
                }

                if(item.getItemId() == R.id.nav_personal)
                {
                    startActivity(new Intent(MainActivity.this, personal.class));
                }

If you getting the same issue. Let me know by commenting

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文