0

while following along with a tutorial my app continuously kept crashing while trying to open another Activity through the floating action button. I'm not able to make out what's going on, so I kindly request someone to assist me with this issue. I'd appreciate it if you could send a detailed explanation and code if possible since I'm still a beginner there's not much I can understand with just a simple answer

Error

2021-11-06 16:46:03.395 18435-18435/com.example.memorymarker E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.memorymarker, PID: 18435
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.memorymarker/com.example.memorymarker.AddPlaceActivity}: android.view.InflateException: Binary XML file line #41: Binary XML file line #41: Error inflating class com.google.android.material.textfield.TextInputLayout
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
        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:1808)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
     Caused by: android.view.InflateException: Binary XML file line #41: Binary XML file line #41: Error inflating class com.google.android.material.textfield.TextInputLayout
     Caused by: android.view.InflateException: Binary XML file line #41: Error inflating class com.google.android.material.textfield.TextInputLayout
     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: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.rInflate(LayoutInflater.java:866)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:866)
        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 android.view.LayoutInflater.inflate(LayoutInflater.java:374)
        at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:699)
        at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:195)
        at com.example.memorymarker.AddPlaceActivity.onCreate(AddPlaceActivity.kt:12)
        at android.app.Activity.performCreate(Activity.java:7136)
        at android.app.Activity.performCreate(Activity.java:7127)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
        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:1808)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
2021-11-06 16:46:03.395 18435-18435/com.example.memorymarker E/AndroidRuntime: 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:243)
        at com.google.android.material.internal.ThemeEnforcement.checkMaterialTheme(ThemeEnforcement.java:217)
        at com.google.android.material.internal.ThemeEnforcement.checkCompatibleTheme(ThemeEnforcement.java:145)
        at com.google.android.material.internal.ThemeEnforcement.obtainTintedStyledAttributes(ThemeEnforcement.java:115)
        at com.google.android.material.textfield.TextInputLayout.<init>(TextInputLayout.java:469)
        at com.google.android.material.textfield.TextInputLayout.<init>(TextInputLayout.java:428)
            ... 32 more

Second activity that I'm trying to enter

package com.example.memorymarker

import android.app.ActionBar
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.appcompat.app.ActionBarDrawerToggle
import androidx.appcompat.widget.Toolbar

class AddPlaceActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_add_place)

        //for back button on action bar
        var toolbar = findViewById<Toolbar>(R.id.toolbar_add_place)
        setSupportActionBar(toolbar)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        toolbar.setNavigationOnClickListener {
            onBackPressed()
        }
    }
}

Second activity xml

<?xml version="1.0" encoding="utf-8"?>
<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="match_parent"
    tools:context=".AddPlaceActivity">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar_add_place"
        android:background="@color/colorPrimary"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:theme="@style/CustomToolBarStyle"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <ScrollView
        android:id="@+id/sv_main"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:fillViewport="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/toolbar_add_place">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="@dimen/main_content_padding">

            <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/til_title"
                style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent">

                <androidx.appcompat.widget.AppCompatEditText
                    android:id="@+id/et_title"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="@string/edit_text_hint_title"
                    android:inputType="textCapWords"
                    android:textColor="@color/primary_text_color"
                    android:textColorHint="@color/secondary_text_color"
                    android:textSize="@dimen/edit_text_text_size" />
            </com.google.android.material.textfield.TextInputLayout>

            <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/til_description"
                style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/add_screen_til_marginTop"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/til_title">

                <androidx.appcompat.widget.AppCompatEditText
                    android:id="@+id/et_description"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="@string/edit_text_hint_description"
                    android:inputType="textCapSentences"
                    android:textColor="@color/primary_text_color"
                    android:textColorHint="@color/secondary_text_color"
                    android:textSize="@dimen/edit_text_text_size"/>
            </com.google.android.material.textfield.TextInputLayout>

            <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/til_date"
                style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/add_screen_til_marginTop"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/til_description">

                <androidx.appcompat.widget.AppCompatEditText
                    android:id="@+id/et_date"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="@string/edit_text_hint_date"
                    android:inputType="text"
                    android:textColor="@color/primary_text_color"
                    android:textColorHint="@color/secondary_text_color"
                    android:textSize="@dimen/edit_text_text_size" />
            </com.google.android.material.textfield.TextInputLayout>

            <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/til_location"
                style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/add_screen_til_marginTop"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/til_date">

                <androidx.appcompat.widget.AppCompatEditText
                    android:id="@+id/et_location"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="@string/edit_text_hint_location"
                    android:inputType="text"
                    android:textColor="@color/primary_text_color"
                    android:textColorHint="@color/secondary_text_color"
                    android:textSize="@dimen/edit_text_text_size" />
            </com.google.android.material.textfield.TextInputLayout>

            <androidx.appcompat.widget.AppCompatImageView
                android:id="@+id/iv_place_image"
                android:layout_width="@dimen/add_screen_place_image_size"
                android:layout_height="@dimen/add_screen_place_image_size"
                android:layout_marginTop="@dimen/add_screen_place_image_marginTop"
                android:background="@drawable/shape_image_view_border"
                android:padding="@dimen/add_screen_place_image_padding"
                android:scaleType="centerCrop"
                android:src="@drawable/add_screen_image_placeholder"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.0"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/til_location" />

            <TextView
                android:id="@+id/tv_add_image"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/add_screen_text_add_image_marginTop"
                android:background="?attr/selectableItemBackground"
                android:gravity="center"
                android:padding="@dimen/add_screen_text_add_image_padding"
                android:text="@string/text_add_image"
                android:textColor="@color/colorAccent"
                android:textSize="@dimen/add_screen_text_add_image_textSize"
                android:textStyle="bold"
                app:layout_constraintBottom_toTopOf="@+id/btn_save"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.45"
                app:layout_constraintStart_toEndOf="@+id/iv_place_image"
                app:layout_constraintTop_toBottomOf="@+id/til_location"
                app:layout_constraintVertical_bias="0.019" />

            <Button
                android:id="@+id/btn_save"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="@dimen/add_screen_btn_save_marginTop"
                android:background="@drawable/shape_button_rounded"
                android:gravity="center"
                android:paddingTop="@dimen/add_screen_btn_save_paddingTopBottom"
                android:paddingBottom="@dimen/add_screen_btn_save_paddingTopBottom"
                android:text="@string/btn_text_save"
                android:textColor="@color/white"
                android:textSize="@dimen/btn_text_size"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/iv_place_image" />
        </androidx.constraintlayout.widget.ConstraintLayout>
    </ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>

gradle

plugins {
    id 'com.android.application'
    id 'kotlin-android'
}

android {
    compileSdk 31

    defaultConfig {
        applicationId "com.example.memorymarker"
        minSdk 28
        targetSdk 31
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
}

dependencies {

    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.3.1'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.1'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
BAVAN M Y
  • 29
  • 2
  • Does this answer your question? [Error : IllegalArgumentException: The style on this component requires your app theme to be Theme.MaterialComponents](https://stackoverflow.com/questions/53476115/error-illegalargumentexception-the-style-on-this-component-requires-your-app) – vadiole Nov 06 '21 at 12:15
  • Can you share you style file? – Zain Nov 06 '21 at 12:32
  • @Zain I just figured out the problem was in the styles file,everything is working good now.Thank you – BAVAN M Y Nov 06 '21 at 12:58

1 Answers1

0

you can change

androidx.appcompat.widget.AppCompatEditText 

to

com.google.android.material.textfield.TextInputEditText

For example:

               <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/et_date"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/edit_text_hint_date"
                android:inputType="text"
                android:textColor="@color/primary_text_color"
                android:textColorHint="@color/secondary_text_color"
                android:textSize="@dimen/edit_text_text_size" /> 
Tanveer Hasan
  • 247
  • 2
  • 9