我需要隐藏 BottomNavigationView
在 SplashScreen 中,这是 Fragment
。
我正在实现导航组件,因此我试图将其限制为仅使用一个 Activity
,我认为这是我所遇到的问题的一部分。
以英里为单位 FragmentSplashScreen
我有:
public class FragmentSplashScreen extends Fragment {
private BottomNavigationView bottomNavigationView;
public View onCreateView(LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_splash_screen, container, false);
bottomNavigationView = v.findViewById(R.id.navview_bottom);
bottomNavigationView.setVisibility(View.GONE);
return v;
}
}
我认为在使用 fragment 及其底层时我误解了一些东西Activity
(抱歉我的英语不好)
编辑
添加 ActivityMain
的布局
<androidx.constraintlayout.widget.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".viewPackage.ActivityMain">
<fragment
android:id="@+id/nav_host_fragment_login"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:navGraph="@navigation/nav_graph" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/navview_bottom"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
app:itemIconTint="@color/colorAccent"
app:itemTextColor="@color/colorAccent"
app:layout_constraintBottom_toBottomOf="@+id/nav_host_fragment_login"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0"
app:menu="@menu/bottom_nav">
</com.google.android.material.bottomnavigation.BottomNavigationView>
</androidx.constraintlayout.widget.ConstraintLayout>
最佳答案
问题是您正在尝试更新 navview_bottom
,这是另一个 View
。
当您调用时:
View v = inflater.inflate(R.layout.fragment_splash_screen, container, false);
v
就是 fragment_splash_screen
及其中包含的任何内容。它不知道 Activity
本身。
要更新Activity
的布局,您可以通过几种不同的方式来执行此操作。
最简单的方法是获取 Activity
的句柄,并使用该句柄更新其布局。
在您的 fragment 中:
((MyMainActivity)context).hideTabBar();
并将该函数添加到您的Activity
中。
public void hideTabBar() {
BottomNavigationView bottomNavigationView = findViewById(R.id.navview_bottom);
bottomNavigationView.setVisibility(View.GONE);
}
关于java - 尝试隐藏 View (BottomNavigationView) 时的 NPE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61332351/