java - 尝试隐藏 View (BottomNavigationView) 时的 NPE

标签 java android android-studio nullpointerexception bottomnavigationview

我需要隐藏 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/

相关文章:

android - 在 gradle、Android Studio 中下载依赖项

java - 在 Ubuntu 16.04 中安装 cassandra 时出现 dpkg oracle Jdk 错误

java - selenium 根据索引位置获取 li 元素并单击复选框

java - POI 3.2 Excel 文件数据丢失

java - 从匿名内部类内部返回其包含的方法的数据

android - 为 PullToRefreshListView 添加标题 View

android - picasso 在使用 View 持有者模式时加载到错误的 ImageView 中

javascript - Android Studio 连接到移动设备中的本地主机

java - Android - 如何生成 GreenDao 实体

java - 获取 HashMap 中元素子集的有效方法是什么?