2

I don't really know what exactly I am missing but I am getting the same inflate exception error even with an Appcomat theme. logcat:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.aasha/com.example.aasha.MainActivity}: android.view.InflateException: Binary XML file line #23: Binary XML file line #23: Error inflating class com.google.android.material.appbar.AppBarLayout
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3139)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3282)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1970)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7156)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975)
     Caused by: android.view.InflateException: Binary XML file line #23: Binary XML file line #23: Error inflating class com.google.android.material.appbar.AppBarLayout
     Caused by: android.view.InflateException: Binary XML file line #23: Error inflating class com.google.android.material.appbar.AppBarLayout

layout:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/chat_good_things">

        <androidx.coordinatorlayout.widget.CoordinatorLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="vertical"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toTopOf="@id/message_box">

            <com.google.android.material.appbar.AppBarLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <androidx.appcompat.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@android:color/transparent"
                    app:layout_scrollFlags="scroll|enterAlways">

                    ....

                </androidx.appcompat.widget.Toolbar>
            </com.google.android.material.appbar.AppBarLayout>

            <androidx.core.widget.NestedScrollView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_behavior="@string/appbar_scrolling_view_behavior">

                <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/chat_recycler_view"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:clipToPadding="false"
                    app:layout_anchor="@id/toolbar"
                    app:layout_anchorGravity="bottom"
                    android:layout_gravity="bottom"/>
            </androidx.core.widget.NestedScrollView>

        </androidx.coordinatorlayout.widget.CoordinatorLayout>

        <LinearLayout
            android:id="@+id/message_box"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="10dp"
            android:background="@android:color/black"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent" >

            <ImageButton
                android:id="@+id/image_send_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                app:srcCompat="@drawable/ic_image_add" />

            <EditText
                android:id="@+id/message_type_box"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:padding="10dp"
                android:layout_weight="1"
                android:layout_gravity="center"
                android:focusableInTouchMode="true"
                android:textIsSelectable="true"
                android:hint="Type a message..."
                android:inputType="text" />

            <ImageButton
                android:id="@+id/message_send_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                app:srcCompat="@drawable/ic_message_send"/>
        </LinearLayout>

        <ProgressBar
            android:id="@+id/progress_bar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="visible"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintBottom_toBottomOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>

</layout>

theme:

<style name="Theme.Aasha" parent="Theme.MaterialComponents.DayNight.NoActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/purple_500</item>
        <item name="colorPrimaryVariant">@color/purple_700</item>
        <item name="colorOnPrimary">@color/white</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_700</item>
        <item name="colorOnSecondary">@color/black</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
        <!-- Customize your theme here. -->
    </style>

gradle:


    implementation 'com.google.android.material:material:1.4.0'

    // androidx
    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.2'
    implementation "androidx.recyclerview:recyclerview:1.2.1"
    implementation "androidx.navigation:navigation-fragment-ktx:$navigation_version"
    implementation "androidx.navigation:navigation-ui-ktx:$navigation_version"
    implementation 'de.hdodenhof:circleimageview:3.1.0'

    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

I searched and came across this StackOverflow, tried all the solutions that include:

Invalidate Caches/Restart with no luck. Refactor -> Migrate to AndroidX that says No usages found for the project.

I was puzzled and made a separate project with the same single layout that run without any issues but I am unable to differentiate and find the fault here. Please help!

Ronnie
  • 207
  • 4
  • 11

2 Answers2

1

Don't forget to use Theme.MaterialComponents.Light.NoActionBar (or any Theme.MaterialComponents.*.NoActionBar really) as your parent AppTheme in style.xml

implementation 'com.google.android.material:material:1.0.0'

classpath 'com.android.tools.build:gradle:3.2.0'

change Gradle and material design to latest version

Marsad
  • 859
  • 1
  • 14
  • 35
androi useru
  • 81
  • 10
  • This was already mentioned in the given StackOverflow link. I already upgraded them to the latest. `com.google.android.material:material:1.4.0` and `classpath "com.android.tools.build:gradle:7.0.3"` – Ronnie Dec 09 '21 at 07:22
1

I was using SplashScreenApi which is from androidx.core.splashscreen.SplashScreen library.

I knew that I have to call installSplashScreen() before setContentView() in my MainAcitivty() but what I didn't know was that it should be called even before any viewBinding initialization. It went somehow unnoticed for days for someone inexperienced like me. The app was running smoothly and everything was working well until any material design tag implementation.

Before:

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)

        installSplashScreen()
        setContentView(binding.root)
        ...
}

After:

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        installSplashScreen()
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        ...
}

That was so stupid of me :(. It took me a while cause the logcat was always pointing the setContentView()

Ronnie
  • 207
  • 4
  • 11