在我的Android应用程序中,我创建了NavigationView,但是我不知道为什么我无法选择我所做的菜单。
以下是activity_main.xml
DrawerLayout
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="@+id/main_app_bar"
layout="@layout/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@+id/fragment_containers"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/main_app_bar"/>
</RelativeLayout>
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/nav_view"
android:fitsSystemWindows="true"
android:layout_gravity="start"
app:menu="@menu/main_menu">
</android.support.design.widget.NavigationView>
这是我的MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAuth = FirebaseAuth.getInstance();
mToolbar = (Toolbar) findViewById(R.id.main_app_bar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_menu);
initNavigationView();
if(findViewById(R.id.fragment_containers) != null){
if(savedInstanceState != null){
return;
}
displayView(R.id.nav_profile);
}
}
private void initNavigationView() {
mDrawerLayout = (DrawerLayout) findViewById(R.id.main_drawer);
mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open, R.string.close){
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
}
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
drawerView.bringToFront();
drawerView.requestLayout();
}
};
mDrawerLayout.addDrawerListener(mToggle);
mToggle.syncState();
FirebaseFirestore user_firestore = FirebaseFirestore.getInstance();
navigationView = (NavigationView) findViewById(R.id.nav_view);
headerView = navigationView.inflateHeaderView(R.layout.navigation_header);
headerImage = (CircleImageView) headerView.findViewById(R.id.headerImage);
headerNama = (TextView) headerView.findViewById(R.id.headerNama);
headerNim = (TextView) headerView.findViewById(R.id.headerNim);
String uid = mAuth.getUid();
user_firestore.collection("Users").document(uid).get()
.addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if(task.isSuccessful()){
if(task.getResult().exists()){
String nama = task.getResult().getString("nama");
String nim = task.getResult().getString("nim");
String image = task.getResult().getString("image");
headerNama.setText(nama);
headerNim.setText(nim);
RequestOptions placeholderRequest = new RequestOptions();
placeholderRequest.placeholder(R.drawable.email_photo);
Uri download_image = Uri.parse(image);
Glide.with(headerView).setDefaultRequestOptions(placeholderRequest).load(download_image).into(headerImage);
} else {
Toast.makeText(MainActivity.this, "Data tidak ditemukan!", Toast.LENGTH_LONG).show();
}
} else {
String error = task.getException().getMessage();
Toast.makeText(MainActivity.this, "(Firestore Error) : " + error, Toast.LENGTH_LONG).show();
}
}
});
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(mToggle.onOptionsItemSelected(item)){
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onStart() {
super.onStart();
FirebaseUser currentUser = mAuth.getCurrentUser();
if(currentUser == null){
sendToLogin();
}
}
private void sendToLogin() {
Intent loginIntent = new Intent(MainActivity.this, LoginActivity.class);
startActivity(loginIntent);
finish();
}
public void setToolbarTitle(String title){
getSupportActionBar().setTitle(title);
}
private void signOut() {
mAuth.signOut();
Intent logoutIntent = new Intent(MainActivity.this, LoginActivity.class);
startActivity(logoutIntent);
finish();
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
displayView(item.getItemId());
return true;
}
@Override
public void onBackPressed() {
if(mDrawerLayout.isDrawerOpen(GravityCompat.START)){
mDrawerLayout.closeDrawer(GravityCompat.START);
} else {
if(!viewAtHome){ //if tampilan bukan pada Home View
displayView(R.id.nav_home);
} else {
moveTaskToBack(true);
}
}
}
public void displayView(int viewId){
Fragment fragment = null;
switch(viewId) {
case R.id.nav_home:
fragment = new MainFragment();
viewAtHome = true;
break;
case R.id.nav_profile:
fragment = new ProfileFragment();
viewAtHome = false;
break;
case R.id.nav_out:
signOut();
break;
}
if(fragment != null){
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_containers, fragment);
fragmentTransaction.commit();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.main_drawer);
drawer.closeDrawer(GravityCompat.START);
}
我已经在搜索如何解决问题但仍无法解决此问题。 感谢您的建议,对不起,我的语法很糟糕。