Fragment isolation testing with Material Design Theme - android

I have a setup of single activity, mutli feature module, that looks like this :
/app - A main module that contains the styles resource that inherits the material design ones, and the activity
/menu - Module feature with just a fragment and viewmodel
/other modules...
I am trying to run my Fragment in my Android Tests in isolation, which results in an error that the Material components (in this case CardView) have to have Material Theme as its style. Which does make sense, but I tried that.
Now I get this error
android.view.InflateException: Binary XML file line #21 in com.nikolam.menu.test:layout/menu_item: Binary XML file line #21 in com.nikolam.menu.test:layout/menu_item: Error inflating class com.google.android.material.card.MaterialCardView
Caused by: android.view.InflateException: Binary XML file line #21 in com.nikolam.menu.test:layout/menu_item: Error inflating class com.google.android.material.card.MaterialCardView
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
at android.view.LayoutInflater.createView(LayoutInflater.java:854)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1006)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:961)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1123)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1084)
at android.view.LayoutInflater.inflate(LayoutInflater.java:682)
at android.view.LayoutInflater.inflate(LayoutInflater.java:534)
at com.nikolam.menu.ui.menu.adapter.MenuAdapter.onCreateViewHolder(MenuAdapter.kt:27)
at com.nikolam.menu.ui.menu.adapter.MenuAdapter.onCreateViewHolder(MenuAdapter.kt:16)
at androidx.recyclerview.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:7266)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6397)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6281)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6277)
at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2330)
at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1631)
at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1591)
at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:668)
at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4277)
at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3980)
at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4546)
at android.view.View.layout(View.java:21912)
at android.view.ViewGroup.layout(ViewGroup.java:6260)
at androidx.constraintlayout.widget.ConstraintLayout.onLayout(ConstraintLayout.java:1915)
at android.view.View.layout(View.java:21912)
at android.view.ViewGroup.layout(ViewGroup.java:6260)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
at android.view.View.layout(View.java:21912)
at android.view.ViewGroup.layout(ViewGroup.java:6260)
at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:509)
at android.view.View.layout(View.java:21912)
at android.view.ViewGroup.layout(ViewGroup.java:6260)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
at com.android.internal.policy.DecorView.onLayout(DecorView.java:779)
at android.view.View.layout(View.java:21912)
at android.view.ViewGroup.layout(ViewGroup.java:6260)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:3080)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2590)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1721)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7598)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:966)
at android.view.Choreographer.doCallbacks(Choreographer.java:790)
at android.view.Choreographer.doFrame(Choreographer.java:725)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:951)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.MaterialComponents (or a descendant).
at com.google.android.material.internal.ThemeEnforcement.checkTheme(ThemeEnforcement.java:240)
at com.google.android.material.internal.ThemeEnforcement.checkMaterialTheme(ThemeEnforcement.java:215)
at com.google.android.material.internal.ThemeEnforcement.checkCompatibleTheme(ThemeEnforcement.java:143)
at com.google.android.material.internal.ThemeEnforcement.obtainStyledAttributes(ThemeEnforcement.java:78)
at com.google.android.material.card.MaterialCardView.<init>(MaterialCardView.java:128)
at com.google.android.material.card.MaterialCardView.<init>(MaterialCardView.java:118)
Which is self-explanatory, but here's the issue this is the layout and that it needs to inherit the material design style. Which it does. This is only the issue while running it in an isolated environment (the Tests) it otherwise works fine. I am guessing that the Fragment in a container doesn't have reference to the style, but I then added it to the CardView itself which confuses me further.
#Before
fun setUp() {
launchFragmentInContainer<MenuFragment>()
}
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.card.MaterialCardView
style="#style/Theme.MaterialComponents.DayNight"
android:id="#+id/materialCardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.appcompat.widget.AppCompatTextView
android:id="#+id/name_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#{item.name}"
android:textSize="30dp"
app:layout_constraintHorizontal_bias="0.212"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/options_recycle_view"
adapter="#{adapter}"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/name_textView"
app:layout_constraintTop_toTopOf="#+id/materialCardView" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
App theme is referenced in the manifest. And the style for the whole app is only in the app module, the Menu module doesn't have its style.xml file. The app contains the only activity of the project. I tried to find others having the same issue but no luck.

FragmentScenario API allows you to explicitly set desired theme for fragment container.
Having an application theme defined in styles.xml as AppTheme with attribute parent=Theme.MaterialComponents, a possible MenuFragment launch invocation would be as follows:
FragmentScenario.launchInContainer(MenuFragment.class, null, R.style.AppTheme, null);
Refer to FragmentScenario documentation for an explanation of the rest of the arguments that are outside of the scope of this answer.

Related

How can I solve inflating class error at Kotlin?

I'm trying to add payment from for my application. I use that library for that. I added it to my project. I'm also try delete from gradle and re-install it but it gave same error again.
Here my xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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="match_parent"
tools:context=".OdemeSayfasi">
<com.fevziomurtekin.payview.Payview
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/payview"/>
</LinearLayout>
In run time I got error which is:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.slidework3, PID: 23708
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.slidework3/com.example.slidework3.OdemeSayfasi}: android.view.InflateException: Binary XML file line #11: Binary XML file line #11: Error inflating class com.fevziomurtekin.payview.Payview
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3253)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)
at android.app.ActivityThread.access$1100(ActivityThread.java:221)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: android.view.InflateException: Binary XML file line #11: Binary XML file line #11: Error inflating class com.fevziomurtekin.payview.Payview
at android.view.LayoutInflater.inflate(LayoutInflater.java:551)
at android.view.LayoutInflater.inflate(LayoutInflater.java:429)
at android.view.LayoutInflater.inflate(LayoutInflater.java:380)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:696)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:170)
at com.example.slidework3.OdemeSayfasi.onCreate(OdemeSayfasi.kt:22)
at android.app.Activity.performCreate(Activity.java:6876)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3206)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349) 
at android.app.ActivityThread.access$1100(ActivityThread.java:221) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:158) 
at android.app.ActivityThread.main(ActivityThread.java:7224) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
This error is very long so I get only first parts of it.
How can I solve it? I try to inflate layout but I can't solve it. I'm waiting for your advices and help.
I tried to replicate it and noticed this message alongside with what you have shown:
Caused by: java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.MaterialComponents (or a descendant).
The reason being the library uses MDC.
I had to change the existing app theme applied into Material Design Component(MDC) based, so to make avoid the error; Example: In under style.xml:
<style name="AppTheme"parent="Theme.MaterialComponents.Light.NoActionBar">
Edit: If you don't want to affect the apptheme, you can just apply a Material Component theme to just the payview.
<com.fevziomurtekin.payview.Payview
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/payview"
android:theme="#style/Theme.MaterialComponents"/>
If you want to customise your payview, just create a custom theme whose parent based on Theme.MaterialComponents .

How to fix ViewPager class inflating exception which is reproducable only with the released Google Play Store version

I converted an application not so long ago from older AppCompat to AndroidX. I tested my updates in my developer environment and all looked good. However to my surprise the clean install of the app from the Google Play Store crashes right after start. I cannot reproduce the crash even with a cleanly wiped cold booted emulator neither with a debug build or a release build.
Here is the source code of the whole app: https://github.com/gdgfresno/androidify-yourself
(the README.md refers to the pre AndroidX state, don't mind that) and the Store listing https://play.google.com/store/apps/details?id=com.valleydevfest.androidify
When I reproduce the crash with the store distributed app I get this crash on the call stack:
2020-07-30 11:02:28.417 31599-31599/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.valleydevfest.androidify, PID: 31599
android.view.InflateException: Binary XML file line #30 in com.valleydevfest.androidify:layout/fragment_main: Binary XML file line #30 in com.valleydevfest.androidify:layout/fragment_main: Error inflating class android.support.v4.view.ViewPager
Caused by: android.view.InflateException: Binary XML file line #30 in com.valleydevfest.androidify:layout/fragment_main: Error inflating class android.support.v4.view.ViewPager
Caused by: java.lang.ClassNotFoundException: android.support.v4.view.ViewPager
at java.lang.Class.classForName(Native Method)
at java.lang.Class.forName(Class.java:454)
at android.view.LayoutInflater.createView(LayoutInflater.java:815)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1006)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:961)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1123)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1084)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1126)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1084)
at android.view.LayoutInflater.inflate(LayoutInflater.java:682)
at android.view.LayoutInflater.inflate(LayoutInflater.java:534)
at com.valleydevfest.androidify.PlaceholderFragment.onCreateView(PlaceholderFragment.java:48)
at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2600)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:881)
at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1303)
at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:439)
at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManagerImpl.java:2079)
at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1869)
at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1824)
at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1727)
at androidx.fragment.app.FragmentManagerImpl.dispatchStateChange(FragmentManagerImpl.java:2663)
at androidx.fragment.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManagerImpl.java:2613)
at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:246)
at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:542)
at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:201)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1514)
at android.app.Activity.performStart(Activity.java:7838)
at android.app.ActivityThread.handleStartActivity(ActivityThread.java:3398)
at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:221)
at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:201)
at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:173)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2109)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:516)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
Caused by: java.lang.ClassNotFoundException: android.support.v4.view.ViewPager
at java.lang.Class.classForName(Native Method) 
at java.lang.Class.forName(Class.java:454) 
at android.view.LayoutInflater.createView(LayoutInflater.java:815) 
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1006) 
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:961) 
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1123) 
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1084) 
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1126) 
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1084) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:682) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:534) 
at com.valleydevfest.androidify.PlaceholderFragment.onCreateView(PlaceholderFragment.java:48) 
at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2600) 
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:881) 
at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238) 
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1303) 
at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:439) 
at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManagerImpl.java:2079) 
at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1869) 
at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1824) 
at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1727) 
at androidx.fragment.app.FragmentManagerImpl.dispatchStateChange(FragmentManagerImpl.java:2663) 
at androidx.fragment.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManagerImpl.java:2613) 
at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:246) 
at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:542) 
at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:201) 
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1514) 
at android.app.Activity.performStart(Activity.java:7838) 
at android.app.ActivityThread.handleStartActivity(ActivityThread.java:3398) 
at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:221) 
at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:201) 
at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:173) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2109) 
at android.os.Handler.dispatchMessage(Handler.java:107) 
at android.os.Looper.loop(Looper.java:214) 
at android.app.ActivityThread.main(ActivityThread.java:7682) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:516) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950) 
The problem is that the execution runtime for some reason searches for the pre-AndroidX ViewPager android.support.v4.view.ViewPager however that should be androidx.viewpager.widget.ViewPager.
THis is what in my layout https://github.com/gdgfresno/androidify-yourself/blob/master/src/main/res/layout/fragment_main.xml
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_weight="90"
android:layout_width="match_parent"
android:layout_height="0dp"
android:padding="1dp"
android:orientation="vertical"
android:background="#drawable/border">
<androidx.viewpager.widget.ViewPager
android:id="#+id/viewPagerHead"
android:layout_weight="30"
android:layout_width="match_parent"
android:layout_height="0dp" />
<androidx.viewpager.widget.ViewPager
android:id="#+id/viewPagerBody"
android:layout_weight="30"
android:layout_width="match_parent"
android:layout_height="0dp" />
<androidx.viewpager.widget.ViewPager
android:id="#+id/viewPagerLegs"
android:layout_weight="30"
android:layout_width="match_parent"
android:layout_height="0dp" />
</androidx.appcompat.widget.LinearLayoutCompat>
just as in my code https://github.com/gdgfresno/androidify-yourself/blob/master/src/main/java/com/valleydevfest/androidify/PlaceholderFragment.java
...
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.viewpager.widget.ViewPager;
...
final View rootView = inflater.inflate(R.layout.fragment_main, container, false);
mViewPagerHead = rootView.findViewById(R.id.viewPagerHead);
mViewPagerBody = rootView.findViewById(R.id.viewPagerBody);
mViewPagerLegs = rootView.findViewById(R.id.viewPagerLegs);
FragmentManager fm = getActivity().getSupportFragmentManager();
mViewPagerHead.setAdapter(new AndroidifyViewPagerAdapter(fm, AndroidDrawables.getHeads()));
mViewPagerBody.setAdapter(new AndroidifyViewPagerAdapter(fm, AndroidDrawables.getBodies()));
mViewPagerLegs.setAdapter(new AndroidifyViewPagerAdapter(fm, AndroidDrawables.getLegs()));
The crash happens at the final View rootView = inflater.inflate(R.layout.fragment_main, container, false); line. I wiped my gradle cache (rm -rf $HOME/.gradle/cache). I wiped the emulators, I uninstalled apps, I rebuilt the app. Where does it get the android.support.v4.view.ViewPager from when everything is androidx?
I also included the implementation 'androidx.fragment:fragment:1.2.5' package in my build.gradle, although that was not needed for a successful compilation. Project issue filed https://github.com/gdgfresno/androidify-yourself/issues/2
I went ahead and converted ViewPager used by the app to ViewPager2 following this guide and my common sense: https://developer.android.com/training/animation/vp2-migration
Here is the commit: https://github.com/gdgfresno/androidify-yourself/commit/df74fc3f0f2d98c3ae59969db11d3678d57a923a
Apparently this removed all ambiguity and chance for the underlying frameworks to pull out an inappropriately old ViewPager "rabbit from the magic hat".

InflateException with Custom RecyclerView in Android

For a project of mine, I wanted to build a custom recyclerview. So, I have extended the RecyclerView the following way:
class MyCustomRecyclerView : RecyclerView {
constructor(context: Context) : super(context){
initializationCode(context)
}
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs){
initializationCode(context)
}
...
}
For the sake of brevity, I excluded the most parts of the custom RecyclerView. As you can see, I also added the required constructors as secondary constructors in Kotlin.
Especially, the constructor with the AttributeSet which is crucial for such custom view implementations. Now, the XML that gets inflated looks like this:
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="postListViewModel"
type="com.celik.abdullah.myproject.viewmodels.PostFragmentViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.fragments.PostFragment">
<com.celik.abdullah.myproject.util.MyCustomRecyclerView
android:id="#+id/custom_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:padding="6dp"
android:clipToPadding="false"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:postListData="#{postListViewModel.postList}"
tools:listitem="#layout/post_list_item">
</com.celik.abdullah.myproject.util.MyCustomRecyclerView>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
But I get the following exception:
Process: com.celik.abdullah.myproject, PID: 4282
android.view.InflateException: Binary XML file line #17: Binary XML file line #17: Error inflating class com.celik.abdullah.myproject.util.MyCustomRecyclerView
Caused by: android.view.InflateException: Binary XML file line #17: Error inflating class com.celik.abdullah.myproject.util.MyCustomRecyclerView
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
at android.view.LayoutInflater.createView(LayoutInflater.java:647)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at androidx.databinding.DataBindingUtil.inflate(DataBindingUtil.java:126)
at androidx.databinding.DataBindingUtil.inflate(DataBindingUtil.java:95)
at com.celik.abdullah.myproject.ui.fragments.PostFragment.onCreateView(PostFragment.kt:35)
at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2600)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:881)
at androidx.fragment.app.FragmentManagerImpl.addAddedFragments(FragmentManagerImpl.java:2100)
at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1874)
at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1830)
at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1727)
at androidx.fragment.app.FragmentManagerImpl$2.run(FragmentManagerImpl.java:150)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Can someone help ?
It looks like your initializationCode throws exception during inflation. InvocationTargetException wraps en exceptions thrown by constructor.
You're using layoutManager property and you're setting androidx's LinearLayoutManager
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
Try removing this from XML and set it from Kotlin file dynamically
Do double check if you are extending RecyclerView in MyCustomRecyclerView from androidx's RecyclerView or not.
As, RecyclerView is available outside androidx also
Please check androidX and withoutAndroidX version for reference

BottomNavigationView inflating error. Resources$NotFoundException: Resource ID #0x0

I'm having some problem with BottomNavigationView from design support library.
I am using it in my activity_main.xml:
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigation"
app:itemIconSize="16dp"
app:labelVisibilityMode="labeled"
app:itemTextColor="#color/white"
app:itemIconTint="#color/white"
app:itemBackground="#color/medium_purple"
app:menu="#menu/main"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
The app crashes when inflating that xml. The full stack trace of the exception is below:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.frolo.musp.dev/com.alexfrolov.mp.main.MainActivity}: android.view.InflateException: Binary XML file line #16: Binary XML file line #16: Error inflating class android.support.design.widget.BottomNavigationView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2814)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1613)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6651)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:822)
Caused by: android.view.InflateException: Binary XML file line #16: Binary XML file line #16: Error inflating class android.support.design.widget.BottomNavigationView
Caused by: android.view.InflateException: Binary XML file line #16: Error inflating class android.support.design.widget.BottomNavigationView
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
at android.view.LayoutInflater.createView(LayoutInflater.java:651)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:794)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:734)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:867)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:828)
at android.view.LayoutInflater.inflate(LayoutInflater.java:519)
at android.view.LayoutInflater.inflate(LayoutInflater.java:427)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.alexfrolov.mp.main.MainActivity.onCreate(MainActivity.java:165)
at android.app.Activity.performCreate(Activity.java:7074)
at android.app.Activity.performCreate(Activity.java:7065)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1215)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2767)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1613)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6651)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:822)
Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x0
at android.content.res.ResourcesImpl.getValue(ResourcesImpl.java:220)
at android.content.res.MiuiResourcesImpl.getValue(MiuiResourcesImpl.java:93)
at android.content.res.Resources.getColorStateList(Resources.java:1034)
at android.content.Context.getColorStateList(Context.java:631)
at android.support.v7.content.res.AppCompatResources.getColorStateList(AppCompatResources.java:67)
at android.support.design.internal.BottomNavigationMenuView.createDefaultColorStateList(BottomNavigationMenuView.java:467)
at android.support.design.internal.BottomNavigationMenuView.<init>(BottomNavigationMenuView.java:101)
at android.support.design.internal.BottomNavigationMenuView.<init>(BottomNavigationMenuView.java:86)
The versions of all support libs and the version of compileSdk are same. I really don't understand what a hell is that. Can anybody explain me please?
Thanks
I had this problem and it baffled me for a day or so. Basically, the BottomNav wants a colorstatelist defined for textColorSecondary in your theme as opposed to a just plain color. So I went from:
<style name="MTheme" parent="Theme.MaterialComponents.Light.Bridge">
...
<item name="android:textColorSecondary">#295055</item>
to
<style name="MTheme" parent="Theme.MaterialComponents.Light.Bridge">
...
<item name="android:textColorSecondary">#color/secondary_textcolor</item>
where secondary_textcolor equals
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true" android:color="#295055"/>
<item android:state_enabled="false" android:color="#color/disabled"/>
</selector>
I wasted a lot of time adding and removing dependencies trying to find this solution.
The problem is in your question title
Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x0
It means that you are using a resource that is not existent. I cannot know which it is but this is how you will fix. Go to the file #menu/main and remove all attributes for icons. You can leave the text. Add one by one to check which one is the problem. It is most likely to be that your icons have been mistakenly placed inside the folder drawable-24 instead of drawable or a higher android API folder than the one you are testing on. Sometimes we make those mistakes.
Also note that some attributes only apply starting a certain android API. Consider checking the attributes starting with app.
app:itemTextColor="#color/white"
app:itemIconTint="#color/white"
app:itemBackground="#color/medium_purple"

Android.view.InflateException: Binary XML file line #25: Error inflating class ImageButton, Resource NotFoundException, XmlPullParserException

In API: 19 Iam facing this Inflate Exception but Iam using AppCompatDrawableManager which supports as vector drawables in API:19
three Exceptions
1. Error in Inflate Image button
2. Resources$NotFoundException File res/drawable/bckg_icon.xml from drawable resource
3. org.xmlpull.v1.XmlPullParserException: Binary XML file line #1: invalid drawable tag vector
I have tried to decerese the ImageButton drawable src which four formats(hdpi,mdpi,xxhdpi etc)
my Implementation :
xml File which is fragmnet xml layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/layoutPreviewActionBar"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/previewFragment"
android:layout_width="fill_parent"
android:layout_height="#dimen/action_bar_height"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="25dp"
android:layout_marginLeft="10dp"
android:text="00:00"
android:id="#+id/timeduration"
android:textColor="#color/colorPrimary"/>
<ImageButton
android:id="#+id/localVideo"
android:layout_width="#dimen/icon_width"
android:layout_height="#dimen/icon_height"
android:layout_centerVertical="true"
android:layout_marginRight="14.1dp"
android:layout_toLeftOf="#+id/call"
android:background="#drawable/bckg_icon"
android:layout_margin="5dp"
android:src="#drawable/video_icon" />
<ImageButton
android:id="#+id/call"
android:layout_width="#dimen/icon_width"
android:layout_height="#dimen/icon_height"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#drawable/initiate_call_button"
android:clickable="true"
android:layout_margin="5dp"
app:srcCompat="#drawable/start_call" />
<ImageButton
android:id="#+id/localAudio"
android:layout_width="#dimen/icon_width"
android:layout_height="#dimen/icon_height"
android:layout_centerVertical="true"
android:layout_marginLeft="14.1dp"
android:layout_toRightOf="#+id/call"
android:background="#drawable/bckg_icon"
app:srcCompat="#drawable/mic_icon" />
</RelativeLayout>
My Fragment java class those drawables implementation :
rootView = inflater.inflate(R.layout.preview_actionbar_fragment, container, false);
mAudioBtn = (ImageButton)rootView.findViewById(R.id.localAudio);
mVideoBtn = (ImageButton)rootView.findViewById(R.id.localVideo);
mCallBtn = (ImageButton)rootView.findViewById(R.id.call);
timdura= (TextView)rootView.findViewById(R.id.timeduration);
c=mActivity;
booking_id=mActivity.bookingId;
drawableStartCall = AppCompatDrawableManager.get().getDrawable(mActivity,R.drawable.initiate_call_button);
drawableEndCall =AppCompatDrawableManager.get().getDrawable(mActivity,R.drawable.end_call_button);
drawableBckBtn = AppCompatDrawableManager.get().getDrawable(mActivity,R.drawable.bckg_icon);
My logCat details:
I/com. .MainActivity: OnAttach PreviewControlFragment
I/com.therightdoctors.dpmdoctor.MainActivity: OnCreate PreviewControlFragment
D/AndroidRuntime: Shutting down VM
W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x41a22cf8)
W/dalvikvm: threadid=1: uncaught exception occurred
E/UncaughtException: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.therightdoctors.dpmdoctor/com.therightdoctors.dpmdoctor.MainActivity}: android.view.InflateException: Binary XML file line #25: Error inflating class ImageButton
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2377)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2429)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1342)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5333)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #25: Error inflating class ImageButton
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at com.therightdoctors.dpmdoctor.ui.PreviewControlFragment.onCreateView(PreviewControlFragment.java:165)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2192)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1299)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:758)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2363)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2149)
at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2103)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2013)
at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:388)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:607)
at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:178)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1174)
at android.app.Activity.performStart(Activity.java:5353)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2340)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2429) 
at android.app.ActivityThread.access$800(ActivityThread.java:151) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1342) 
at android.os.Handler.dispatchMessage(Handler.java:110) 
at android.os.Looper.loop(Looper.java:193) 
at android.app.ActivityThread.main(ActivityThread.java:5333) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:515) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645) 
at dalvik.system.NativeStart.main(Native Method) 
Caused by: android.content.res.Resources$NotFoundException: File res/drawable/bckg_icon.xml from drawable resource ID #0x7f02005c
at android.content.res.Resources.loadDrawable(Resources.java:2170)
at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
at android.view.View.<init>(View.java:3579)
at android.widget.ImageView.<init>(ImageView.java:123)
at android.widget.ImageButton.<init>(ImageButton.java:87)
at android.support.v7.widget.AppCompatImageButton.<init>(AppCompatImageButton.java:60)
at android.support.v7.widget.AppCompatImageButton.<init>(AppCompatImageButton.java:56)
at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:118)
at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1029)
at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1087)
at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:47)
at android.view.LayoutInflater$FactoryMerger.onCreateView(LayoutInflater.java:172)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:684)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755) 
at android.view.LayoutInflater.rInflate(LayoutInflater.java:758) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:492) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 
at com.therightdoctors.dpmdoctor.ui.PreviewControlFragment.onCreateView(PreviewControlFragment.java:165) 
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2192) 
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1299) 
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528) 
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595) 
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:758) 
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2363) 
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2149) 
at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2103) 
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2013) 
at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:388) 
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:607) 
at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:178) 
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1174) 
at android.app.Activity.performStart(Activity.java:5353) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2340) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2429) 
at android.app.ActivityThread.access$800(ActivityThread.java:151) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1342) 
at android.os.Handler.dispatchMessage(Handler.java:110) 
at android.os.Looper.loop(Looper.java:193) 
at android.app.ActivityThread.main(ActivityThread.java:5333) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:515) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645) 
at dalvik.system.NativeStart.main(Native Method) 
Caused by: org.xmlpull.v1.XmlPullParserException: Binar
I have tried from StackOverflow some similar Question not working
to reduce the images sizes ,
2.use app:srcCompat instead of android:src attribute in XML
problem only in API:19 lower DPI devices how to Handle these type of Exceptions
I know it's been a while but I think that you are using a VectorDrawable as background of your ImageButton in here:
<ImageButton
android:id="#+id/localVideo"
android:layout_width="#dimen/icon_width"
android:layout_height="#dimen/icon_height"
android:layout_centerVertical="true"
android:layout_marginRight="14.1dp"
android:layout_toLeftOf="#+id/call"
android:layout_margin="5dp"
android:src="#drawable/video_icon"
android:background="#drawable/bckg_icon />" <!-- <- here is your problem -->
I figured out that using VectorDrawable as background of TextView, EditText, Button, ImageButton, etc using android:background="..." cause a crash on API 19 and before, Because android:background on older devices is using old APIs and when android tries to inflate your VectorDrawable it can not parse it and crashes. Note that when you are using app:srcCompat as the source image of your ImageView you are using Compat library and there is no problem.
For fixing this you can use one of 3 methods:
Use an ImageView behind your view and set its app:srcCompat to your VectorDrawable
Use a custom widget that loads VectorDrawable using Compat library in runtime and sets the background
Use alternative resources to set background of your View to your VectorDrawable in API 21 and above and set it to a BitmapDrawable (png file for example) in lower APIs.
Once I had faced same issue, You can try anyone of the following methods :
Use .png or .jpeg instead of xml, and make sure the drawables are copied to drawable folder ( sometime it copies to drawable(v21) folder and then these images will not be available for API below 21).
Check the same thing for .xml drawables, in which directory it is kept.(You can view directory structure by changing view from Android to Project
Use srcCompat instead of src in xml files. You may need to declare
compile 'com.android.support:support-vector-drawable:23.4.0'

Categories

Resources