1

Strange Exception I am getting on a system app when I am using just FrameLayout in my SampleActivity.

Caused by: java.lang.ClassCastException: androidx.appcompat.widget.ContentFrameLayout cannot be cast to android.support.v7.widget.ContentFrameLayout
05-22 08:41:05.357 10520 10520 E AndroidRuntime: at android.support.v7.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:685)
05-22 08:41:05.357 10520 10520 E AndroidRuntime: at android.support.v7.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:516)
05-22 08:41:05.357 10520 10520 E AndroidRuntime: at android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:464)
05-22 08:41:05.357 10520 10520 E AndroidRuntime: at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
05-22 08:41:05.357 10520 10520 E AndroidRuntime: at com.sample.SampleActivity.onCreate(SampleActivity.java:98)

So I looked into the source code of AppCompatDelegateImpl present here : AppCompatDelegateImpl

imports in SampleActivity.java:

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

While this class is part of android appcompat.v7 but internally it is importing androidx.appcompat.widget.ContentFrameLayout Does it imply that appcompat.v7 internally use androidx dependencies ? If yes, then why I am getting this exception ?

Note: Please avoid giving gradle based solutions since it is a system app.

rekire
  • 47,260
  • 30
  • 167
  • 264
TheLittleNaruto
  • 8,325
  • 4
  • 54
  • 73

3 Answers3

3

Fix was to include androidx dependencies in Android.mk file.

Also had to change all import of support library classes to androidx as suggested in Android developer site: https://developer.android.com/jetpack/androidx/migrate

TheLittleNaruto
  • 8,325
  • 4
  • 54
  • 73
1

Go to gradle.properties file and make sure

android.useAndroidX=false

If you set this to true, android plugin will use AndroidX Library instead of Support Library. Furthermore, if you're unable to find useAndroidX in gradle.properties, try adding it and perform gradle sync.

Bugs 404
  • 69
  • 2
  • @TheLittleNaruto The **ContentFrameLayout** you have inside SampleActivity is part of [androidx.appcompat.widget](https://android.googlesource.com/platform/frameworks/support/+/master/v7/appcompat/src/main/java/androidx/appcompat/widget/ContentFrameLayout.java). The **ContentFrameLayout** you need is inside [android.support.v7.widget](https://android.googlesource.com/platform/frameworks/support/+/8c3f757/v7/appcompat/src/android/support/v7/widget/ContentFrameLayout.java). Try using `import android.widget.FrameLayout;` and see if it links to support library. – Bugs 404 May 23 '19 at 10:07
  • I am using this FrameLayout only. – TheLittleNaruto May 23 '19 at 10:31
0

Update your import statements. You're using class with same name but from different packages.

Said
  • 689
  • 6
  • 20