I'm trying to transition from using a FrameLayout
to using FragmentContainerView
and from the tutorials I've seen, it should be pretty easy. However, when I run my app after making the change, I get
Error inflating class androidx.fragment.app.FragmentContainerView
Caused by: java.lang.ClassNotFoundException: androidx.fragment.app.FragmentContainerView
my layout file is pretty simple:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="@layout/app_bar_main">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragment_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
Do I need to add something into my build.gradle or something? Why can't it find the class?
android.widget.FrameLayout. ↳ androidx.fragment.app.FragmentContainerView. FragmentContainerView is a customized Layout designed specifically for Fragments. It extends FrameLayout , so it can reliably handle Fragment Transactions, and it also has additional features to coordinate with fragment behavior.
Another way to simply add a Fragment to FragmentContainerView is by using the attribute android:name=”fragment_class_name_path" in XML. Both the attributes android:name or class are similar things we just need to give the classpath as a value to inflate the fragment.
NavHostFragment provides an area within your layout for self-contained navigation to occur. NavHostFragment is intended to be used as the content area within a layout resource defining your app's chrome around it, e.g.: <androidx.drawerlayout.widget.DrawerLayout.
Android Studio kept recommending that I use FragmentContainerView instead of a Fragment in my layout file for most of this year. The problem is that I could never get things to work properly, so I kept using the <fragment> tag in my main Activity's layout file, activity_main.xml.
The reason is when the inflater inflates FrgmentContainer, it calls default constructor to initialize the fragments. If their is no default constructor available, it will crash. If you are using kotlin the latest FragmentContainerView implementation stays under:
I'm trying to transition from using a FrameLayout to using FragmentContainerView and from the tutorials I've seen, it should be pretty easy. However, when I run my app after making the change, I get
When a Fragment’s view is being created, a tag is assigned to it in order to associate it with the Fragment instance, this is done by a call to View.setTag (R.id. fragment_container_view_tag, fragment). When FragmentContainerView.addView (View) is called, the view’s tag is retrieved by calling View.getTag (R.id. fragment_container_view_tag).
That's in androidx.fragment
1.2.0 or higher:
implementation "androidx.fragment:fragment:1.2.0"
Thanks to @Salam El-Banna and his link, I found that in case an application crashes in release
build we should add in proguard-rules.pro
(one or two lines, depending on android:name
in <FragmentContainerView>
:
#-------------------------------------------------
# JetPack Navigation
# This fixes:
# Caused by: androidx.fragment.app.Fragment$InstantiationException: Unable to instantiate fragment androidx.navigation.fragment.NavHostFragment: make sure class name exists
# Caused by: androidx.fragment.app.Fragment$InstantiationException: Unable to instantiate fragment com.google.android.gms.maps.SupportMapFragment: make sure class name exists
#-------------------------------------------------
-keepnames class androidx.navigation.fragment.NavHostFragment
-keepnames class com.google.android.gms.maps.SupportMapFragment
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With