因此,我目前正在制作一個應用程式,并使用側邊選單來實作一些功能,如注銷、設定等。但是,當用戶需要注銷時,我必須將他重定向到LoginActivity,但由于某些原因,它只是關閉了側邊選單,就像該專案上沒有任何東西。 我在這里和youtube上都找過類似的問題,但都無濟于事。另外,當按下側邊選單上的 "logout_btn "時,應用程式似乎沒有錯誤
MainActivity:
public class MainActivity extends AppCompatActivity implements NavigationView. OnNavigationItemSelectedListener {
private BottomNavigationView bottomNav;
private DrawerLayout root;
private Toolbar 工具列。
private TextView report_btn;
private NavigationView navigationView;
private String[] permissions;
private LocationManager locationManager;
private FirebaseAuth mAuth;
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch(item.getItemId()){
case R.id.logout_btn:
mAuth.signOut()。
startActivity(new Intent(MainActivity.this, LoginActivity.class)) 。
break。
}
return true。
}
@Override; }
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(s savedInstanceState)。
setContentView(R.layout.activity_main)。
//確保沒有資料被留下。
ServiceSharedInfo.deleteALlData()。
mAuth = FirebaseAuth.getInstance();
//Setting values[/span]。
FirebaseUser user = mAuth.getCurrentUser()。
if(user == null){
startActivity(new Intent(MainActivity.this, LoginActivity.class)) 。
}else{
System.out.println("#~#~#~#~#~# User: " user.getEmail())。
}
permissions = new String[]{Manifest.permission.CAMERA, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.INTERNET];
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
root = findViewById(R.id.root_layout);
toolbar = findViewById(R.id.main_tool_bar);
navigationView = findViewById(R.id.side_nav);
bottomNav = findViewById(R.id.bottom_navigation);
bottomNav.setOnNavigationItemSelectedListener(navListener)。
report_btn = findViewById(R.id.report_btn);
setSupportActionBar(toolbar)。
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(MainActivity. this,
root,toolbar,R.string.navigation_open,
R.string.navigation_close)。)
root.addDrawerListener(toggle);
toggle.syncState();
getSupportFragmentManager().beginTransaction().replace(R.id.container_layout,
new HomeFragment()).commit()。
bottomNav.getMenu().getItem(1).setChecked(true)。
report_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent emailIntent = new Intent(Intent.action_SEND)。
emailIntent.setType("plain/text")。
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{"[email protected]"/span>});
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "<-Your subject here->") 。
emailIntent.putExtra(Intent.EXTRA_TEXT, "<-Your Problem here->")。
startActivity(Intent.createChooser(emailIntent,"發送郵件..."))。
}
});
navigationView.setNavigationItemSelectedListener(this::onNavigationItemSelected) 。
}
private BottomNavigationView.OnNavigationItemSelectedListener navListener =
new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override。
public boolean onNavigationItemSelected(@NonNull MenuItem item){
Fragment fragmentSelected = null;
switch(item.getItemId()){
case R.id.home_nav:
fragmentSelected = new HomeFragment() 。
break。
case R.id.installation_nav:
fragmentSelected = new InstallationFragment();
break。
case R.id.damage_nav:
fragmentSelected = new DamageFragment();
break。
}
getSupportFragmentManager().beginTransaction().replace(R.id.container_layout,
fragmentSelected).commit()。
return true。
}
};
}
activity_main:
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"/span>?
xmlns:app="http://schemas.android.com/apk/res-auto"/span>
xmlns:tools="http://schemas.android.com/tools"/span>
android:layout_width="match_parent"/span>
android:layout_height="match_parent"/span>
tools:context=".MainActivity"。
android:id="@ id/root_layout"。
android:fitSystemWindows="true">
<com.google.android.material.navigation.NavigationView
android:id="@ id/side_nav"。
android:layout_width="wrap_content"/span>
android:layout_height="match_parent"/span>
android:layout_gravity="start"/span>
app:headerLayout="@layout/menu_header"/span>
app:menu="@menu/side_nav_menu"。
tools:visibility="消失" />
<RelativeLayout
android:layout_width="match_parent"/span>
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"/span>
android:layout_height="wrap_content"/span>
android:background="@color/white"/span>
android:id="@ id/appBar">
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"/span>
android:layout_height="wrap_content"/span>
android:id="@ id/main_tool_bar">
<RelativeLayout
android:layout_width="match_parent"/span>
android:layout_height="wrap_content">
<ImageView
android:layout_width="100dp"/span>
android:layout_height="30dp"/span>
android:layout_alignParentStart="true"/span>
android:src="@drawable/somephoto" />
<TextView
android:id="@ id/report_btn"/span>
android:layout_width="wrap_content"/span>
android:layout_height="wrap_content"/span>
android:layout_alignParentEnd="true"/span>
android:drawableStart="@drawable/ic_baseline_priority_high_24" />
</RelativeLayout>
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
<FrameLayout
android:id="@ id/container_layout"/span>
android:layout_width="match_parent"。
android:layout_height="match_parent"/span>
android:layout_above="@id/bottom_navigation"/span>
android:layout_below="@id/appBar" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@ id/bottom_navigation"/span>
android:layout_width="match_parent"/span>
android:layout_height="wrap_content"/span>
android:layout_alignParentBottom="true"/span>
android:background="@color/white"/span>
android:clickable="false"/span>
app:menu="@menu/button_navigation" />
</RelativeLayout>
</androidx.drawerlayout.widget.DrawerLayout>
side_nav_menu:
<?xml version="1.0" encoding="utf-8"?>
<選單 xmlns:android="http://schemas.android.com/apk/res/android"?
xmlns:tools="http://schemas.android.com/tools"?
tools:showIn="navigation_view">
<組>。
<專案
android:id="@ id/logout_btn"/span>
android:title="Log out :(")
android:icon="@drawable/ic_baseline_exit_to_app_24"/span>/>
</group>
</menu>
uj5u.com熱心網友回復:
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item){
if (item.getTitle().equals("Log out :("/span>)) {
mAuth.signOut()。
startActivity(new Intent(MainActivity.this, LoginActivity.class)) 。
return false。
}
這對我有用。
試試這個...
uj5u.com熱心網友回復:
我找了一整天,發現側邊選單中的專案不被點擊的原因是你必須在xml檔案中的最后一個關閉屬性之前加上 "NavigationView"。不知道為什么,但它起作用了!
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/333335.html
標籤:
下一篇:歐拉-馬舍羅尼常數
