-1

I have a Kotlin app and it crashing after I create some XML files.

I think problem is in the Fragment, but I can see it. When I try a Fragment Tab, app is not crashing, but I don't need an Fragment Tab, only a Fragment with Bottom Navigation View.

In kt classes I don't have code yet, until this moment I have work only XML files. Before add Fragment the app not crashing.

Please, anyone can help me? Thanks!

XML code is following:

Activiy_main.xml

    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>```


1 Answers1

0
<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>

Use fragment instead of FragmentContainerView. I think that is the root of your problem

mirsaidoff
  • 116
  • 4
  • Android Studio said fragment is not the current way to use Fragments. I tried and give me more problems. In this moment I see something that could be the problem. Check this image: https://drive.google.com/file/d/10XlQGuDi7qZZWg7_zWASlEcvkQQMAYyM/view?usp=sharing – Moisés Gómez Aug 14 '20 at 18:44
  • @MoisésGómez yes, the Lint will say that you are not using the optimal way. But this is the reality, FragmentContainerView crashes. It should be fixed. Try and let me know if it helped. – mirsaidoff Aug 14 '20 at 18:45
  • I tried with , but it also crashed! I just realized the following: https://drive.google.com/file/d/10XlQGuDi7qZZWg7_zWASlEcvkQQMAYyM/view?usp=sharing In the red zone it suppose to say activity_main(nav_hos_fragment), but it is not and i don't know why.It could be the problem. What do you say? – Moisés Gómez Aug 14 '20 at 18:52