ClassNotFoundException when using androidx.fragment.app.FragmentContainerView - android

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?

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

Make sure their are no custom constructors like this.
ex:
FragmentName (val ctx: Context): Fragment() {
change to
FragmentName: Fragment() {
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:
implementation("androidx.fragment:fragment-ktx:1.3.6")

Related

SnackbarContentLayout class not found after migration to AndroidX

After migration to AndroidX my app crashes after calling Snackbar.make() method. The error looks like
Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class android.support.design.internal.SnackbarContentLayout
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.design.internal.SnackbarContentLayout"
It's strange that internal Snackbar method make() is still trying to access SnackbarContentLayout class in old support package instead new material's one. Please advice what I'm doing wrong.
UPDATE
Finally I found a solution. There is a custom layout in my project which overrides
Snackbar's design_layout_snackbar_include and it had wrong class in root view tag:
<view
xmlns:android="http://schemas.android.com/apk/res/android"
class="android.support.design.internal.SnackbarContentLayout"
android:theme="#style/ThemeOverlay.AppCompat.Dark"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom">
I changed it to com.google.android.material.snackbar.SnackbarContentLayout
ensure that your project is set up to migrate old packages to Androidx by including this in your gradle.properties file:
android.useAndroidX=true
android.enableJetifier=true

Didn't find class "androidx.constraintlayout.ConstraintLayout"

I am developing a library for Android, and this library has some activities with its layouts.
In the layout, I have the next code
...
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.floatingactionbutton.FloatingActionButton
...
I am using Androidx.
An I get the following error:
Error inflating class android.support.constraint.ConstraintLayout
The first attempt to solve this was to changeandroid.support.constraint.ConstraintLayout to androidx.constraintlayout.widget.ConstraintLayout
But when I do this I get the following error:
androidx.constraintlayout.widget.ConstraintLayout cannot be cast to
androidx.constraintlayout.widget.Group
So I try to change to androidx.constraintlayout.widget.Group
I get the following error:
androidx.constraintlayout.widget.Group cannot be cast to
android.view.ViewGroup
I did a refactor again to androidx and then I get the following error:
Didn't find class "androidx.constraintlayout.ConstraintLayout"
Any idea?
Thanks
Follow this steps
First add below dependencies in your Build.Gradle file
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
Use this ConstraintLayout like this
<androidx.constraintlayout.widget.ConstraintLayout 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="wrap_content">
</androidx.constraintlayout.widget.ConstraintLayout>
Make sure you have correct imports of ConstraintLayout for in your activity if you have used
import androidx.constraintlayout.widget.ConstraintLayout

java.lang.ClassNotFoundException in android studio

i am trying to add module and but its gvien error like this:
Caused by: java.lang.ClassNotFoundException: Didn't find class
"com.anghatechsolution.eww.datetimepicker.SlidingTabStrip" on path:
DexPathList[[zip file
"/data/app/com.anghatechsolution.eww.datetimepicker-1/base.apk"],nativeLibraryDirectories=[/vendor/lib,
/system/lib]]
can any one having idea about this.
This error means, that you are trying to access class, that currently doesn't exist.
Try to check the name of class, you accessing
You can solve it by setting the name attribute to your class like this:
[Activity(Name = "somepackage.custombuttonrenderer")]
public class CustomButtonRenderer: ButtonRenderer
{ }
An explanation is in my answer here: Why is my custom ButtonRenderer not working?

Android Error inflating class TextInputLayout

I'm trying to use TextInputLayout in my XML, and I'm meeting this error :
java.lang.RuntimeException: Unable to start activity ComponentInfo{fr.jechange.moncompte/fr.jechange.moncompte.SignUpActivity}: android.view.InflateException: Binary XML file line #36: Error inflating class android.support.design.widget.TextInputLayout
[...]
Caused by: android.view.InflateException: Binary XML file line #36: Error inflating class android.support.design.widget.TextInputLayout
[...]
Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/design/R$styleable;
I've imported the design library, and the support-v7-appcompat library. And I'm using the Theme.AppCompat.Light.NoActionBar theme.
The XML in which I'm using the Text Input Layout contains just something like that:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:res="http://schemas.android.com/apk/res-auto"
style="#style/matchParent">
<android.support.design.widget.TextInputLayout
android:id="#+id/input"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:inputType="text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.TextInputLayout>
</RelativeLayout>
if you are calling layout(containg TextInputLayout) from activity then
nothing just use YouActivityClassName.this in place of getApplicationcontext or YourApplicationClassName.getcontext() or other where you are passing context
I had the same issue fixed here.
Make sure you don't have any other support library import v4 and v7. You can use only the design-support library. Also you should make sure your theme "matchParent" inherit from Theme.AppCompat for all the platform you trying to run.
My issue was only on Lollipop because of the values-21/style.xml.
Hope it will help.

R file for custom component not compiling

I have two custom components, one which I made quite recently, both in Android Studio. The earlier one worked. I followed the same procedure to make the new one but it throws runtime errors when I reach the activity screen during testing.
These are the exception messages, listed in order:
1. java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sentient/com.mycompany.MyActivity}: android.view.InflateException: Binary XML file line #6: Error inflating class <unknown>
2. Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class <unknown>
3. java.lang.reflect.InvocationTargetException
4. java.lang.NoClassDefFoundError: com.mycomponent.R$color
Error #4 shows the actual line itself that throws the error, it is part of the constructor of the component:
int color = res.getColor(R.color.progress_gray);
I checked the R file manually, and found the colors (progress_gray is defined in the colors.xml) to be present. I removed the line, and another line throws the error instead (which again depends on the R file to get information).
I have tried cleaning, changing build order in gradle, Make the component separately, and I still can't figure out where the mistake is. Any help is appreciated! Thanks.
can you post the xml file of the custom view?
Check if you have added to your main layout of the custom view xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
I got the solution. The applicationId in build.gradle and the package in the manifest were not matching. Once I changed it, it worked.
Strange though, I had thought that the reason the two were separated was so that they could be named differently. In any case, that was what got it working.

Categories

Resources