Search code examples
javaandroidandroid-studionullpointerexceptionbottomnavigationview

NPE when trying to hide View (BottomNavigationView)


I need to hide a BottomNavigationView in a SplashScreen, which is a Fragment. I'm implementing Navigation Component, so I'm trying to limitate it to use only one Activity, and I think that is part of the problem that I have.

in mi FragmentSplashScreen I have:

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;
    }
}

I think that I'm misunderstanding something when working with fragments and its underlaying Activity

(Sorry for my bad english)

EDIT

Add Layout of the 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>


Solution

  • The problem is you're trying to update navview_bottom, which is another View.

    When you call:

    View v = inflater.inflate(R.layout.fragment_splash_screen, container, false);
    

    v is that just fragment_splash_screen and anything contained within it. It does not know about the Activity itself.

    To update the Activity's layout, you can do this a few different ways.

    The simplest would be to get a handle to the Activity, and have a method on that to update it's layout.

    Within your Fragment:

    ((MyMainActivity)context).hideTabBar();
    

    And add the function to your Activity.

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