java.lang.IllegalStateException: Fragment class was not set
来源:3-6 app定制Fragment导航器

慕莱坞42732
2020-11-13
NavController navController;
AppBottomBar navView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
navView = findViewById(R.id.nav_view);
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment);
navController = NavHostFragment.findNavController(fragment);
NavGraphBuilder.build(this, fragment.getChildFragmentManager(), navController, fragment.getId());
navView.setOnNavigationItemSelectedListener(this);
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
navController.navigate(menuItem.getItemId());
return !TextUtils.isEmpty(menuItem.getTitle());
}
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app=“http://schemas.android.com/apk/res-auto"
android:id=”@+id/container"
android:layout_width=“match_parent"
android:layout_height=“match_parent"
android:paddingTop=”?attr/actionBarSize”>
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@id/nav_view"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.guoanrui.mynavigation.view.AppBottomBar
android:id="@+id/nav_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
老师您好,总是报这个故障,你帮我看下问题出在哪里那
3回答
-
banmahy
2020-11-24
同问,请问作者怎么解决的??
00 -
慕莱坞42732
提问者
2020-11-16
老师我找到问题在哪了,我按照你的思路在NavGraphBuilder中,由于项目刚建立的时候是三个fragment,后来destnation.json变成4个fragment和一个activity,数量对应不上,已修改完,可以运行了
00 -
LovelyChubby
2020-11-13
你就是创建destination 对象的时候.fragment class没设置进去,可以断在navigationGrapBuilder看下
00
相似问题