我正在使用the following example中的BottomNavigationDrawer
/ BottomSheetDialog
我在主Activity
中运行了三个片段,我在使用FragmentTransactions
和SupportFragmentManager
之间进行导航。
FragmentRevivIncidentDashboard
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_reviv_incident_dashboard, container, false);
setRetainInstance(true);
// initialize variables
email = ((Reviv) getActivity()).getEmail();
apikey = ((Reviv) getActivity()).getApikey();
// set up UI elements
menuButton = ((Reviv)getActivity()).getFab();
menuButton.setImageResource(R.drawable.add_white);
bottomAppBar = ((Reviv)getActivity()).getBottom_app_bar();
bottomAppBar.setFabAlignmentMode(BottomAppBar.FAB_ALIGNMENT_MODE_CENTER);
navigationView = ((Reviv) getActivity()).getNavigationView();
FragmentSettingsMain
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate (R.layout.fragment_revive_settings, container, false);
((Reviv) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
setRetainInstance(true);
// Activity Views and viewModel
bottomAppBar = ((Reviv)getActivity()).getBottom_app_bar();
bottomAppBar.setFabAlignmentMode(BottomAppBar.FAB_ALIGNMENT_MODE_END);
navigationView = ((Reviv) getActivity()).getNavigationView();
if(navigationView == null) navigationView = ((Reviv) getActivity()).getNavigationView();
menuButton = ((Reviv)getActivity()).getFab();
menuButton.setImageResource(R.drawable.baseline_undo_white_24dp);
menuButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(navigationView == null) navigationView = ((Reviv) getActivity()).getNavigationView();
navigationView.getMenu().findItem(R.id.app_bar_incident).setChecked(true);
((Reviv) getActivity()).getSupportFragmentManager()
.beginTransaction()
.replace(R.id.containerFrameLayout, new FragmentRevivIncidentDashboard()).commit();
}
});
活动-Reviv.class
BottomNavigationDrawerFragment fragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reviv);
uberLayout = findViewById(R.id.uberLayout);
floatingActionButton = findViewById(R.id.floatingActionButton);
// Initialize all the location related objects here
if (ContextCompat.checkSelfPermission(this,
android.Manifest.permission.INTERNET)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{android.Manifest.permission.INTERNET},
MY_PERMISSIONS_ACCESS_INTERNET);
}
// ask for permissions
displayLocationSettingsRequest(getApplicationContext());
beingLocationUpdation(this);
bottom_app_bar = findViewById(R.id.bottom_app_bar);
setSupportActionBar(bottom_app_bar);
fragment = new BottomNavigationDrawerFragment();
navigationView = fragment.getNavigationView();
*Elsewhere in the main Activity*
public NavigationView getNavigationView() {
if(fragment == null) {
fragment = new BottomNavigationDrawerFragment();
navigationView = fragment.getNavigationView();
} else navigationView = fragment.getNavigationView();
return navigationView;
}
底页片段
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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/bottomsheet_menu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:alpha="1">
<android.support.constraint.ConstraintLayout
android:id="@+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@+id/txtUsername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="4dp"
android:text="User Name"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:textColor="@color/label_text_light"
android:textSize="14sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@+id/imageView4"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.3" />
<ImageView
android:id="@+id/imageView4"
android:layout_width="63dp"
android:layout_height="65dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/baseline_face_white_48" />
<TextView
android:id="@+id/txtEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:text="Email Address"
android:textColor="@color/colorText"
android:textSize="10sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="@+id/txtUsername"
app:layout_constraintTop_toBottomOf="@+id/txtUsername"
app:layout_constraintVertical_bias="0.0" />
<ImageView
android:id="@+id/imgClose"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="16dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/baseline_close_white_48dp" />
</android.support.constraint.ConstraintLayout>
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:itemTextAppearance="@style/TextAppearance.AppCompat.Body2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/constraintLayout"
app:itemIconTint="@color/drawer_item"
app:itemTextColor="@color/drawer_item"
app:itemBackground="@drawable/menu_background_color"
app:menu="@menu/bottom_nav_drawer_menu">
</android.support.design.widget.NavigationView>
</android.support.constraint.ConstraintLayout>
问题
当我使用该按钮从“设置片段”中导航回默认(第一个)片段时,菜单不会更新。
尝试从片段中获取对Main活动中定义的navigationView的引用,这给了我NPE,然后调用
menuButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(navigationView == null) navigationView = ((Reviv) getActivity()).getNavigationView();
navigationView.getMenu().findItem(R.id.app_bar_incident).setChecked(true);
((Reviv) getActivity()).getSupportFragmentManager()
.beginTransaction()
.replace(R.id.containerFrameLayout, new FragmentRevivIncidentDashboard()).commit();
}
});
不会导致任何可见变化,即使正在显示IncidentDashboard片段,“设置”菜单项也会保持选中状态。
甚至
menuButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(navigationView == null) navigationView = ((Reviv) getActivity()).getNavigationView();
navigationView.getMenu().findItem(R.id.app_bar_incident).setChecked(true);
Menu bottomNavigationMenu = navigationView.getMenu();
bottomNavigationMenu.performIdentifierAction(R.id.app_bar_incident, 0);
/*navigationView.setCheckedItem(R.id.default);*/
/*((Reviv) getActivity()).getSupportFragmentManager()
.beginTransaction()
.replace(R.id.containerFrameLayout, new FragmentRevivIncidentDashboard()).commit();*/
}
});
在第二种情况下导致NPE,在第一种情况下没有可见的菜单选择。
java.lang.NullPointerException:尝试调用虚拟方法 'android.support.v4.app.FragmentManager android.support.v4.app.FragmentActivity.getSupportFragmentManager()' 在空对象引用上
设置
navigationView.setCheckedItem(R.id.app_bar_incident);
目标(事件仪表板)中的片段会在navigationView上生成NPE。
由于:java.lang.NullPointerException:尝试调用虚拟 方法“无效” android.support.design.widget.NavigationView.setCheckedItem(int)' 空对象引用 在com.package.class.Fragments.FragmentRevivIncidentDashboard.onCreateView(FragmentRevivIncidentDashboard.java:130)
我见过this SO question和this one,但它们似乎没有帮助。
如何从片段主菜单中选择导航菜单项?
[UPDATE]同样在THIS ANSWER上找到了KeepassXC's source code,但尽管感觉像是正确的,但它不起作用。