英文:
Android crash onCreate() - Fragment cannot be cast to androidx.navigation.fragment.NavHostFragment
问题
I decided to execute a Firebase Robo Test for my app and noticed that it always crashes after opening an external activity and then returning back to the app. I was able to duplicate the problem by enabling the "Don't keep activities" in the Developer options.
Crash: java.lang.ClassCastException: com.example.problemtesting.Fragments.Fragment_2 cannot be cast to androidx.navigation.fragment.NavHostFragment
It indicates that the problem is caused by this line of code: NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment);
How to duplicate the problem:
- Enable "Don't keep activities"
 - Open the app and then the app drawer
 - Select "Fragment 2"
 - Press on the Share button
 - Press on the Messages button (or any other external app)
 - Return to the app (Crash)
 
The app works fine if "Don't keep activities" is disabled.
MainActivity
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
    // ... (remaining code)
}
Fragment 1
public class Fragment_1 extends Fragment {
    // ... (remaining code)
}
Fragment 2
public class Fragment_2 extends Fragment {
    // ... (remaining code)
}
activity_main.xml
<!-- ... (remaining code) -->
mobile_navigation.xml
<!-- ... (remaining code) -->
main_drawer.xml
<!-- ... (remaining code) -->
AndroidManifest.xml
<!-- ... (remaining code) -->
It's a frustrating problem that I can't figure out. I have tried to remove the NavigationView.OnNavigationItemSelectedListener listener and it works fine, but I need that listener for other controls such as fragment switching animations and drawer layout transitions.
英文:
I decided to execute a Firebase Robo Test for my app and noticed that it always crashes after opening an external activity and then returning back to the app. I was able to duplicate the problem by enabling the "Don't keep activities" in the Developer options.
Crash: java.lang.ClassCastException: com.example.problemtesting.Fragments.Fragment_2 cannot be cast to androidx.navigation.fragment.NavHostFragment
It indicates that the problem is caused by this line of code: NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment);
How to duplicate the problem:
- Enable "Don't keep activities"
 - Open the app and then the app drawer
 - Select "Fragment 2"
 - Press on the Share button
 - Press on the Messages button (or any other external app)
 - Return to the app (Crash)
 
The app works fine if "Don't keep activities" is disabled.
MainActivity
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
    DrawerLayout drawer;
    NavigationView navigationView;
    FragmentManager fragmentManager;
    AppBarConfiguration mAppBarConfiguration;
    NavController navController;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        fragmentManager = getSupportFragmentManager();
        drawer = findViewById(R.id.drawer_layout);
        navigationView = findViewById(R.id.nav_view);
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        mAppBarConfiguration = new AppBarConfiguration.Builder(
                R.id.nav_fragment_1, R.id.nav_fragment_2)
                .setOpenableLayout(drawer)
                .build();
        NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment);
        if (navHostFragment != null)
            navController = navHostFragment.getNavController();
        else navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(MainActivity.this, navController, mAppBarConfiguration);
        NavigationUI.setupWithNavController(navigationView, navController);
        setNavigationViewListener();
    }
    @Override
    public boolean onSupportNavigateUp() {
        return NavigationUI.navigateUp(navController, mAppBarConfiguration)
                || super.onSupportNavigateUp();
    }
    private void setNavigationViewListener() {
        navigationView.setNavigationItemSelectedListener(MainActivity.this);
    }
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
        switch (menuItem.getItemId()) {
            case R.id.nav_fragment_1: {
                fragmentManager.beginTransaction()
                        .replace(R.id.nav_host_fragment, new Fragment_1())
                        .commitNow();
                MainActivity.this.setTitle("Fragment 1");
                break;
            }
            case R.id.nav_fragment_2: {
                fragmentManager.beginTransaction()
                        .replace(R.id.nav_host_fragment, new Fragment_2())
                        .commitNow();
                MainActivity.this.setTitle("Fragment 2");
                break;
            }
        }
        drawer.closeDrawers();
        return true;
    }
}
Fragment 1
public class Fragment_1 extends Fragment{
    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        container.removeAllViews();
        View root = inflater.inflate(R.layout.fragment_1, container, false);
        return root;
    }
}
Fragment 2
public class Fragment_2 extends Fragment {
    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        container.removeAllViews();
        View root = inflater.inflate(R.layout.fragment_2, container, false);
        Button share = root.findViewById(R.id.share);
        share.setOnClickListener(view -> {
            Intent intent = new Intent(Intent.ACTION_SEND);
            intent.setType("text/plain");
            intent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
            intent.putExtra(Intent.EXTRA_TEXT, "Message");
            startActivity(Intent.createChooser(intent, "Title"));
        });
        return root;
    }
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start"
    >
    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:tag="my_fragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:defaultNavHost="true"
        app:navGraph="@navigation/mobile_navigation"
        />
    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/main_drawer"
        />
</androidx.drawerlayout.widget.DrawerLayout>
mobile_navigation.xml
<?xml version="1.0" encoding="utf-8"?>
<navigation 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"
    app:startDestination="@+id/nav_fragment_1">
    <fragment
        android:id="@+id/nav_fragment_1"
        android:name="com.example.problemtesting.Fragments.Fragment_1"
        tools:layout="@layout/fragment_1"
        />
    <fragment
        android:id="@+id/nav_fragment_2"
        android:name="com.example.problemtesting.Fragments.Fragment_2"
        tools:layout="@layout/fragment_2"
        />
</navigation>
main_drawer.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:showIn="navigation_view">
    <group android:checkableBehavior="single">
        <item
            android:id="@+id/nav_fragment_1"
            android:title="Fragment 1" />
        <item
            android:id="@+id/nav_fragment_2"
            android:title="Fragment 2" />
    </group>
</menu>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.problemtesting">
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name="com.example.problemtesting.MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>
It's a frustrating problem that I can't figure out. I have tried to remove the NavigationView.OnNavigationItemSelectedListener listener and it works fine, but I need that listener for other controls such as fragment switching animations and drawer layout transitions.
答案1
得分: 3
问题在于您的 onNavigationItemSelected 正在执行一个 FragmentTransaction,用一个片段替换整个 NavHostFragment。这始终是在使用 NavHostFragment 时更改片段的错误方法 - 您应始终是 导航到目标。
然而,在您的情况下,您做的工作比实际上必要的要多得多。相反,您应该:
- 
完全删除您的
OnNavigationItemSelectedListener代码。当您调用NavigationUI.setupWithNavController(navigationView, navController);时,导航已经为您设置了一个,其中包括关闭抽屉并根据 Material 设计规范使用正确的淡入淡出动画。 - 
根据 顶部应用栏指南,您不应该手动设置活动的标题,而是在图形中的每个目标上添加一个
android:label,然后您已经执行的setupActionBarWithNavController调用将为您执行此操作。 
英文:
The problem is that your onNavigationItemSelected is doing a FragmentTransaction, replacing the entire NavHostFragment with a Fragment. This is always the wrong way to change fragments when using a NavHostFragment - you should always be navigating to a destination.
However, in your case, you are doing way more work than is actually necessary. Instead, you should:
- 
Remove your
OnNavigationItemSelectedListenercode entirely. Navigation has already set one up for you when you callNavigationUI.setupWithNavController(navigationView, navController);That includes closing the drawer and using the correct cross fade animation as per the Material design spec. - 
As per the top app bar guide, you should not be manually setting the title of the acctivity, but instead add an
android:labelto each destination in your graph and thesetupActionBarWithNavControllercall you've done will do that for you. 
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论