BottomNavigation+Fragment适配异常:Android6.0+崩溃,5.0正常
从你给出的崩溃日志来看,核心问题是**BottomNavigationView在Android 6.0+系统中无法被正确找到,导致findViewById(R.id.bottom_navigation)返回null**,进而调用setOnNavigationItemSelectedListener时触发空指针异常。
问题根源
你的根布局用的是CoordinatorLayout,但其中的FrameLayout使用了android:layout_above="@+id/bottom_navigation"——这个属性是RelativeLayout特有的,CoordinatorLayout并不支持该属性。在Android 5.0系统中可能对无效属性的兼容性更好,布局仍能正常解析;但Android 6.0+对布局属性的校验更严格,无效属性会导致视图树解析异常,最终使得BottomNavigationView没有被正确初始化,findViewById自然找不到它。
修复方案
我们需要调整布局,让CoordinatorLayout能正确识别子视图的布局关系,具体步骤如下:
- 移除
FrameLayout的android:layout_above="@+id/bottom_navigation"属性 - 给
FrameLayout添加app:layout_behavior="@string/appbar_scrolling_view_behavior",让它自动适配AppBarLayout的位置(位于AppBarLayout下方) - 给
FrameLayout设置android:layout_marginBottom="56dp"(和BottomNavigationView的高度一致),避免内容被底部导航栏遮挡 - 保留
BottomNavigationView的android:layout_gravity="bottom",确保它固定在屏幕底部
修改后的完整布局代码
<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto" tools:context="com.example.samsung.auto_temperatura.MainActivity"> <android.support.design.widget.AppBarLayout android:id="@+id/appBar" android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/ThemeOverlay.AppCompat.ActionBar"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:contentInsetLeft="0dp" android:elevation="4dp" android:fitsSystemWindows="true" android:minHeight="?attr/actionBarSize" android:popupTheme="@style/ThemeOverlay.AppCompat.Light" android:titleTextColor="@color/bialy" app:contentInsetRight="0dp"> <ImageView android:layout_width="100dp" android:layout_height="40dp" android:src="@drawable/imkapknapis" android:layout_marginRight="12dp"/> <TextView android:id="@+id/toolbar_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginRight="4dp" android:text="STAN CZUJNIKÓW:" android:visibility="visible" android:textStyle="bold"/> <TextView android:id="@+id/stan" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="DOBRY" android:textStyle="bold"/> </android.support.v7.widget.Toolbar> </android.support.design.widget.AppBarLayout> <FrameLayout android:id="@+id/fragment_container" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" android:layout_marginBottom="56dp" /> <android.support.design.widget.BottomNavigationView android:id="@+id/bottom_navigation" android:layout_width="match_parent" android:layout_height="56dp" android:layout_gravity="bottom" app:itemBackground="@color/bialy" app:itemIconTint="@color/imkNiebieski" app:itemTextColor="@color/czarny" app:menu="@menu/nav_items" /> </android.support.design.widget.CoordinatorLayout>
额外优化建议
你在MainActivity中重复定义了两次OnNavigationItemSelectedListener(一个成员变量,一个在onCreate中new的),可以删掉其中一个,直接使用成员变量:
@Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); BottomNavigationView navigation = findViewById(R.id.bottom_navigation); // 直接使用已定义的成员变量,避免重复代码 navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener); FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); ft.add(R.id.fragment_container, CzujnikiFragment.newInstance(), FRAGMENT_CZUJNIKI) .commit(); }
这样修改后,你的BottomNavigation导航功能应该能在所有支持的Android版本上正常运行了。
内容的提问来源于stack exchange,提问作者Brachu
相关产品推荐
相关产品推荐

