导航菜单不适用于其他活动 - Android Studio
我正在为我的应用程序编写导航菜单。该应用程序从登录屏幕开始,然后进入主要活动。从那里,可以在左侧滑动打开导航栏,然后您将进入任何页面。菜单将让我从主要活动转到一页,但在该页面上我可以通过单击主页返回主要活动,但无法转到任何其他活动。这是我第一次在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么您添加
意图= getIntent()
在开始时。getIntent()
用于发送字符串,int,bool
从一个活动到第二活动。如果您只想从第一个活动到第二个活动,只需删除此行即可。请参阅下面的新代码。
如果您遇到同样的问题。通过评论让我知道
Why you added
Intent intent = getIntent()
after startActivity.getIntent()
works for sendingStrings, int, bool
from one Activity to Second Activity.Just remove this line if you just want to go from the first to the second activity. See your new code below.
If you getting the same issue. Let me know by commenting