Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ClassNotFoundException when using androidx.fragment.app.FragmentContainerView

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?

like image 653
Randy Avatar asked Jan 31 '20 18:01

Randy


People also ask

What is Androidx fragment app FragmentContainerView?

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.

How do I use FragmentContainerView?

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.

What is Nav_host_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.

Should I use fragmentcontainerview instead of a fragment in Android Studio?

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.

Why does fragmentcontainerview crash when it is created?

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:

Is it easy to transition from framelayout to fragmentcontainerview?

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

How do I get the tag of a view from a fragment?

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).


2 Answers

That's in androidx.fragment 1.2.0 or higher:

implementation "androidx.fragment:fragment:1.2.0"
like image 94
CommonsWare Avatar answered Oct 11 '22 08:10

CommonsWare


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
like image 36
CoolMind Avatar answered Oct 11 '22 08:10

CoolMind