Home > Software design >  Error inflating class android.appcombat.widget.Toolbar
Error inflating class android.appcombat.widget.Toolbar

Time:04-21

I keep getting "App has stopped working" error in the android studios emulator, these are the errors I am getting:

Caused by: android.view.InflateException: Binary XML file line #10 in com.example.utatrivia:layout/activity_main: Binary XML file line #10 in com.example.utatrivia:layout/activity_main: Error inflating class androidx.appcompat.widget.Toolbar 
Caused by: android.view.InflateException: Binary XML file line #10 in com.example.utatrivia:layout/activity_main: Error inflating class androidx.appcompat.widget.Toolbar
Caused by: java.lang.reflect.InvocationTargetException

The following is a portion of my activity_main.XML file:

<?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"
    android:background="@color/light_grey"
    tools:context=".MainActivity">

    <androidx.appcompat.widget.Toolbar
        android:id="@ id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/background"
        android:minHeight="?attr/actionBarSize"
        android:theme="?attr/actionBarTheme"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <View
        android:id="@ id/view2"
        android:layout_width="wrap_content"
        android:layout_height="80dp"
        android:background="@drawable/background_bottom"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@ id/toolbar" />

    <View
        android:id="@ id/view3"
        android:layout_width="0dp"
        android:layout_height="150dp"
        android:layout_marginStart="32dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="32dp"
        android:background="@drawable/rounded_box"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@ id/toolbar" />

</androidx.constraintlayout.widget.ConstraintLayout>

The following is MainActivity.java file. Just a quick note, one of the errors shown was highlighted in blue and when I clicked it, it led to the "binding = ActivityMainbinding.inflate(getLayoutInflater());" line of the following code:

package com.example.utatrivia;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;

import android.os.Bundle;

import com.example.utatrivia.databinding.ActivityMainBinding;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        ArrayList<CategoryModel> categories = new ArrayList<>();
        categories.add(new CategoryModel("","History",""));
        CategoryAdapter adapter = new CategoryAdapter(this, categories);
        binding.categoryList.setLayoutManager(new GridLayoutManager(this, 2));
        binding.categoryList.setAdapter(adapter);
    }
}

The full stack trace:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.utatrivia, PID: 13088
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.utatrivia/com.example.utatrivia.MainActivity}: android.view.InflateException: Binary XML file line #10 in com.example.utatrivia:layout/activity_main: Binary XML file line #10 in com.example.utatrivia:layout/activity_main: Error inflating class androidx.appcompat.widget.Toolbar
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3685)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3842)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2252)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loopOnce(Looper.java:201)
        at android.os.Looper.loop(Looper.java:288)
        at android.app.ActivityThread.main(ActivityThread.java:7842)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
     Caused by: android.view.InflateException: Binary XML file line #10 in com.example.utatrivia:layout/activity_main: Binary XML file line #10 in com.example.utatrivia:layout/activity_main: Error inflating class androidx.appcompat.widget.Toolbar
     Caused by: android.view.InflateException: Binary XML file line #10 in com.example.utatrivia:layout/activity_main: Error inflating class androidx.appcompat.widget.Toolbar
     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:858)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1010)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:965)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:1127)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1088)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:686)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:538)
        at com.example.utatrivia.databinding.ActivityMainBinding.inflate(ActivityMainBinding.java:101)
        at com.example.utatrivia.databinding.ActivityMainBinding.inflate(ActivityMainBinding.java:95)
        at com.example.utatrivia.MainActivity.onCreate(MainActivity.java:19)
        at android.app.Activity.performCreate(Activity.java:8054)
        at android.app.Activity.performCreate(Activity.java:8034)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1341)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3666)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3842)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2252)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loopOnce(Looper.java:201)
        at android.os.Looper.loop(Looper.java:288)
        at android.app.ActivityThread.main(ActivityThread.java:7842)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
     Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 37: TypedValue{t=0x2/d=0x7f040007 a=-1}
E/AndroidRuntime:     at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:783)
        at android.view.View.<init>(View.java:5805)
        at android.view.ViewGroup.<init>(ViewGroup.java:702)
        at android.view.ViewGroup.<init>(ViewGroup.java:698)
        at androidx.appcompat.widget.Toolbar.<init>(Toolbar.java:236)
        at androidx.appcompat.widget.Toolbar.<init>(Toolbar.java:232)
            ... 28 more

Please let me know if any other files need to be uploaded.

CodePudding user response:

Try removing minHeight then It should work.

CodePudding user response:

I think you are using data binding but you are not converted your XML file into data binding. you should wrap your parent layout into a layout tag to use data binding.

<?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"
xmlns:tools="http://schemas.android.com/tools">

<data>

</data>

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/light_grey"
    tools:context=".MainActivity">

    <androidx.appcompat.widget.Toolbar
        android:id="@ id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/background"
        android:minHeight="?attr/actionBarSize"
        android:theme="?attr/actionBarTheme"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <View
        android:id="@ id/view2"
        android:layout_width="wrap_content"
        android:layout_height="80dp"
        android:background="@drawable/background_bottom"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@ id/toolbar" />

    <View
        android:id="@ id/view3"
        android:layout_width="0dp"
        android:layout_height="150dp"
        android:layout_marginStart="32dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="32dp"
        android:background="@drawable/rounded_box"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@ id/toolbar" />

</androidx.constraintlayout.widget.ConstraintLayout>

and in your java file there is also one change.

binding = DataBindingUtil.setContentView(this,R.layout.activity_main,null);
  • Related