SnackbarContentLayout class not found after migration to AndroidX - android

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

Related

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?
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")

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

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.

Android - HorizontalListView not inflating in XML from Class file

I have added the library to my project.
Then in XML file I am trying to add the HorizontalListView as the following
<com.devsmart.android.ui.HorizontialListView
android:id="#+id/horizontalList"
android:layout_width="284dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:background="#ddd" />
But it gives the following Exception
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.golfer/com.golfer.WeatherScreen}: android.view.InflateException: Binary XML file line #256: Error inflating class com.devsmart.android.ui.HorizontialListView
Caused by: android.view.InflateException: Binary XML file line #256: Error inflating class com.devsmart.android.ui.HorizontialListView
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.devsmart.android.ui.HorizontialListView" on path: DexPathList[[zip file "/data/app/com.golfer-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.golfer-1, /vendor/lib, /system/lib]]
I have followed the instructions on this site
Any one having any idea about it? Any help?
Replace com.devsmart.android.ui.HorizontialListView by the name of your package +name of the class.
Something like this: com.example.android.MyHorizontalView where com.example.android is the name of your package and MyHorizontalView is the name of the class (that you created).
EDIT
From this post and android developer, you are probably missing a line to declare the namespace of the view. Try adding a line like this one
xmlns:widget="http://schemas.android.com/apk/res/com.mypackage"
In the parent layout (below the line xmlns:android="http://schemas.android.com/apk/res/android")
You can try it the following way,
Add devsmartlib.jar to the libs folder.
Then go to Java Build Path - > Libraries and Add JAR devsmartlib.jar to the project.
Did you add the Lib to your Dependencies? If not, add it!
Edit
Eclipse:
Right-Click on Project-> Build Path-> Configure Build Path-> Libraries-> Check if lib is in there, if not -> add Library.....

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