user14536109
user14536109

Reputation:

"Fatal Exception: android.view.InflateException" on some devices

The crash is:

Fatal Exception: android.view.InflateException
Binary XML file line #13: Error inflating class

I've seen on Firebase Crashlytics that some devices get this crash. I tried an emulator and found all APIs lower or equal to 19 have this crash. In Firebase I see all APIs having it including the latest ones.

But anyway, I found what was causing the crash. First here is the layout that gets inflated:

<ScrollView 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">

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"
        android:background="@drawable/dialog_background">

        <!-- Lots of views here -->

    </RelativeLayout>

    <ImageView
        android:id="@+id/iconImageView2"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="0dp"
        android:background="?attr/actionBarItemBackground"
        android:scaleType="fitCenter"
        app:srcCompat="@android:mipmap/sym_def_app_icon"
        tools:ignore="ContentDescription" />
</RelativeLayout>
</ScrollView>

The part that causes the crash is android:background="@drawable/dialog_background". Here is dialog_background:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <corners
        android:radius="30dp" />
    <solid android:color="?attr/dialog_color" />

</shape>

If I replace <solid android:color="?attr/dialog_color"/>with <solid android:color="@color/anything"/> then it will not crash anymore. But I need to use ?attr/dialog_color so that the background color will change based on the selected theme. What can I do to fix this crash?

Upvotes: 0

Views: 105

Answers (1)

private static
private static

Reputation: 770

This is an example. You can change the codes and libraries used depending on your needs. dialog_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="25dp"
    android:paddingTop="25dp"
    android:paddingStart="10dp"
    android:paddingEnd="10dp">

       <!-- Apply your attr to the background here -->
    <android.support.v7.widget.CardView
        android:id="@+id/cardView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="0dp"
        android:layout_centerInParent="true"
        android:background="?attr/colorPrimary"
        app:cardCornerRadius="15dp"
        app:cardUseCompatPadding="true"
        android:minHeight="400dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:orientation="vertical">

            <TextView
                android:id="@+id/txt_title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="12dp"
                android:lines="1"
                android:maxLines="1"
                android:textSize="16sp"
                android:textStyle="bold"
                android:paddingRight="12dp"
                android:paddingTop="16dp"/>

        </LinearLayout>

    </android.support.v7.widget.CardView>

</RelativeLayout>

You might get a strange background behind the CardView. I don't know how you initialize your dialog or what you use but here's an example on how to remove the dialog's background:

Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View p1) {
                    
                    AlertDialog.Builder dialog = new AlertDialog.Builder( MainActivity.this );
                    LayoutInflater inflater = getLayoutInflater();
                    View v = inflater.inflate(R.layout.dialog_layout, null);

                    TextView txtTitle = v.findViewById(R.id.txt_title);
                    txtTitle.setText("This is a dialog");
                    
                    dialog.setView( v );
                    // Make sure you initialize your dialog like this
                    AlertDialog mDialog = dialog.create();
                    mDialog.show();
                    // This line will do the job
                    mDialog.getWindow().setBackgroundDrawable(new  ColorDrawable(android.graphics.Color.TRANSPARENT) );
                }
        });

Dialog with background: enter image description here Dialog without background: enter image description here

Upvotes: 0

Related Questions