试图隐藏视图时的 NPE (BottomNavigationView)

NPE when trying to hide View (BottomNavigationView)

我需要在 SplashScreen 中隐藏一个 BottomNavigationView,这是一个 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;
    }
}

我认为我在使用片段及其底层时误解了一些东西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 的句柄,并在其上有一个方法来更新它的布局。

在你的片段中:

((MyMainActivity)context).hideTabBar();

并将函数添加到您的 Activity

public void hideTabBar() {
    BottomNavigationView bottomNavigationView = findViewById(R.id.navview_bottom);
    bottomNavigationView.setVisibility(View.GONE);
}