在BottomNavigation中快速在它们之间切换时,片段崩溃

时间:2018-08-22 05:44:29

标签: android fragment bottomnavigationview

我的MainActivity里面有BottomNavigation,它可以通过以下方法加载片段

private boolean loadFragment(Fragment fragment) {
        //switching fragment
        if (fragment != null) {
            getSupportFragmentManager()
                    .beginTransaction()
                    .replace(R.id.fragment_container, fragment)
                    .commit();
            return true;
        }
        return false;
    }

当我在它们之间快速切换时,我的应用程序在不同地方崩溃了,但是当我慢慢走一遍时,一切都很好

这就是我的应用程序结构

enter image description here

我正在使用Reteofit进行API调用

我已经尝试setuserVisibleHint并检查是否创建了我的视图rootView

1 个答案:

答案 0 :(得分:0)

我当时正在使用改型来获取API的响应,然后将其传递给适配器,但是由于使用底部导航和片段的快速切换,我的应用程序崩溃了,因为hideProgressBar()抛出NullException。我观察到视图被破坏,然后小部件无法执行。

因此,我进行检查以确保该视图不为空。它解决了我的情况。我希望它能帮助正在寻找相同解决方案的其他人。

在Kotlin中:视图和Java:getView()(在Java中)

enter image description here