Android 当我添加片段时,我的应用程序崩溃。为什么呢?我怎么能修好它?
我有一个Kotlin应用程序,它在我创建一些XML文件后崩溃了 我认为问题在片段中,但我可以看到它。当我尝试一个片段选项卡时,应用程序并没有崩溃,但我不需要一个片段选项卡,只需要一个具有底部导航视图的片段 在kt类中,我还没有代码,直到现在我只有工作XML文件。在添加片段之前,应用程序不会崩溃 拜托,有人能帮我吗?谢谢 XML代码如下所示: Activiy_main.xmlAndroid 当我添加片段时,我的应用程序崩溃。为什么呢?我怎么能修好它?,android,kotlin,android-fragments,Android,Kotlin,Android Fragments,我有一个Kotlin应用程序,它在我创建一些XML文件后崩溃了 我认为问题在片段中,但我可以看到它。当我尝试一个片段选项卡时,应用程序并没有崩溃,但我不需要一个片段选项卡,只需要一个具有底部导航视图的片段 在kt类中,我还没有代码,直到现在我只有工作XML文件。在添加片段之前,应用程序不会崩溃 拜托,有人能帮我吗?谢谢 XML代码如下所示: Activiy_main.xml xmlns:android="http://schemas.android.com/apk/res/an
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"
android:orientation="vertical"
tools:context=".ui.MainActivity">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?colorPrimary"
android:theme="@style/ToolbarTheme" />
<androidx.fragment.app.FragmentContainerView
android:id="@+id/nav_host_fragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:name="androidx.navigation.fragment.NavHostFragment"
app:navGraph="@navigation/mobile_navigation"
app:defaultNavHost="true" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_nav"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:menu="@menu/bottom_nav"/>
</LinearLayout>```
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\u width=“匹配父项”
android:layout\u height=“match\u parent”
android:orientation=“垂直”
工具:context=“.ui.MainActivity”>
```
使用fragment代替FragmentContainerView。我认为这是你问题的根源请分享你的stacktrace以及Android Studio所说的片段不是当前使用片段的方式。我试着给我更多的问题。在这一刻,我看到了一些可能的问题。检查此图像:@MoisésGómez是的,Lint会说您没有使用最佳方式。但这是现实,FragmentContainerView崩溃了。它应该被修复。试着让我知道它是否有用。我试过了,但它也崩溃了!我刚刚意识到:在红色区域,它应该说activity_main(nav_hos_fragment),但它不是,我不知道为什么。这可能是问题所在。你怎么说?
<LinearLayout
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"
android:orientation="vertical"
tools:context=".ui.MainActivity">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?colorPrimary"
android:theme="@style/ToolbarTheme" />
<fragment
android:id="@+id/nav_host_fragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:name="androidx.navigation.fragment.NavHostFragment"
app:navGraph="@navigation/mobile_navigation"
app:defaultNavHost="true" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_nav"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:menu="@menu/bottom_nav"/>
</LinearLayout>