所以我试图为大学项目开发一个应用程序,但是我的代码无法正常运行,所以我制作了一个菜单侧边栏,并使用一些分隔符,例如个人资料,事件,关于我们,常见问题,设置,共享和注销
侧边栏
所以我创建了一些片段
片段
但是,当我单击关于我们或常见问题时,我遇到了关于我们和常见问题的片段问题,它转到设置片段,我不知道为什么? 有人可以帮我吗?
MainActivity.Java
package com.example.readytogo;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Switch;
import android.widget.Toast;
import com.google.android.material.navigation.NavigationView;
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private DrawerLayout drawer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView =findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar,
R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
if(savedInstanceState == null){
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new ProfileFragment()).commit();
navigationView.setCheckedItem(R.id.nav_profile);
}
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId()){
case R.id.nav_profile:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new ProfileFragment()).commit();
break;
case R.id.nav_events:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new EventsFragment()).commit();
case R.id.nav_aboutus:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new AboutUsFragment()).commit();
break;
case R.id.nav_faq:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new FAQFragment()).commit();
break;
case R.id.nav_settings:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new SettingsFragment()).commit();
break;
case R.id.nav_share:
Toast.makeText(this, "Share", Toast.LENGTH_SHORT).show();
break;
case R.id.nav_logout:
Toast.makeText(this, "Logout", Toast.LENGTH_SHORT).show();
break;
}
drawer.closeDrawer(GravityCompat.START);
return true;
}
@Override
public void onBackPressed(){
if (drawer.isDrawerOpen(GravityCompat.START)){
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
}
drawer_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="Navigation_view">
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_profile"
android:icon="@drawable/ic_person"
android:title="Profile" />
<item
android:id="@+id/nav_events"
android:icon="@drawable/ic_event"
android:title="Events" />
<item
android:id="@+id/nav_aboutus"
android:icon="@mipmap/aboutus"
android:title="About us" />
<item
android:id="@+id/nav_faq"
android:icon="@mipmap/faq"
android:title="FAQ" />
<item
android:id="@+id/nav_settings"
android:icon="@drawable/ic_settings"
android:title="Settings" />
</group>
<item android:title="Communicate">
<menu>
<item
android:id="@+id/nav_share"
android:icon="@drawable/ic_share"
android:title="Share" />
</menu>
</item>
<item android:title="Sign in">
<menu>
<item
android:id="@+id/nav_logout"
android:icon="@mipmap/arrow"
android:title="Log out" />
</menu>
</item>
</menu>
答案 0 :(得分:2)
您在有关“常见问题”和“关于我们”页面的单击事件中丢失了。您需要添加..
case R.id.nav_faq: // the id will be the one you are using in your xml
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new FAQFragment()).commit();
break;
case R.id.nav_about_us: // the id will be the one you are using in your xml
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new AboutUsFragment()).commit();
break;
将它们添加到添加其他事件的位置。
答案 1 :(得分:1)
onNavigationItemSelected()
内没有关于About&FAQ的情况。
另外,请确保在nav_view.xml
(不是nav_settings
)内为“关于我们和常见问题解答”设置了正确的ID,这可能是打开SettingsFragment
的可能原因之一。
答案 2 :(得分:0)
make sure you have set correct id for About Us & FAQ inside app>res>menu>
and add these two lines in onNavigationItemSelected Method.I have mentioned below just check and put your id in that switch case
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId()){
case R.id.nav_profile:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new ProfileFragment()).commit();
break;
case R.id.nav_events:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new EventsFragment()).commit();
break;
case R.id.about_us:
Toast.makeText(this, "About us", Toast.LENGTH_SHORT).show();
break;
case R.id.faq:
Toast.makeText(this, "FAQ", Toast.LENGTH_SHORT).show();
break;
case R.id.nav_settings:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new SettingsFragment()).commit();
break;
case R.id.nav_share:
Toast.makeText(this, "Share", Toast.LENGTH_SHORT).show();
break;
case R.id.nav_logout:
Toast.makeText(this, "Logout", Toast.LENGTH_SHORT).show();
break;
}
drawer.closeDrawer(GravityCompat.START);
return true;
}
答案 3 :(得分:0)
我已经检查了更新的代码。剩下的唯一问题是您忘记在nav_events案件中添加break
case R.id.nav_events:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new EventsFragment()).commit();
break;
这现在应该修复您的代码,现在应该可以解决您当前遇到的问题:
事件片段有关我们的所有其他事情都起作用,只有此片段事件不起作用