Android TextInputField Inflator Error

2019-01-11 21:59发布

Had a crash while trying to use the new TextInputField for Android and wanted to share my solution.

Trying the new TextInputField in the android appcompat library was crashing my app. Here was my layout xml.

<android.support.design.widget.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <EditText
        android:id="@+id/email"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="e-mail"
        android:inputType="textEmailAddress"
        android:singleLine="true"/>

</android.support.design.widget.TextInputLayout>

The error I got:

android.view.InflateException: Binary XML file line #20: Error inflating class android.support.design.widget.TextInputLayout.

SOLUTION: Add the hintTextAppearance attribute to your TextInputLayout, so the lead tag looks like this:

<android.support.design.widget.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:hintTextAppearance="@android:style/TextAppearance.Medium">

8条回答
We Are One
2楼-- · 2019-01-11 22:25

Make sure the LayoutInflater is created from an AppCompatActivity

ex.

instead of

inflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

use:

inflater = LayoutInflater.from(<appCompatActivity>);
查看更多
劫难
3楼-- · 2019-01-11 22:27

you might have added it compile 'com.android.support:appcompat-v7:22.2.0' but you need to add compile 'com.android.support:design:22.2.0'

you can try this dependencies:

compile 'com.android.support:appcompat-v7:22.2.0'
compile 'com.android.support:design:22.2.0'

in your gradle file.

查看更多
老娘就宠你
4楼-- · 2019-01-11 22:30

Use the following:

View view = LayoutInflator.from(getActivity()).inflate(R.layout.your_layout_file,parent,false);
查看更多
戒情不戒烟
5楼-- · 2019-01-11 22:31

I had the same issue with Android Studio 3.0.1

All you need to add the following line into gradle.properties (Project Propeties):

android.enableAapt2=false

查看更多
再贱就再见
6楼-- · 2019-01-11 22:32

This happened to me as well, and I came up with a solution that does not require changing the App Theme, but merely changing the Theme of the TextInputLayout:

<android.support.design.widget.TextInputLayout
    android:id="@+id/testingInputLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/Theme.AppCompat">

   <EditText
       android:id="@+id/testingEditText"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:hint="@string/testText"
       android:inputType="textEmailAddress" />

</android.support.design.widget.TextInputLayout>

You will need to add the appCompat library if you have not already:

compile 'com.android.support:appcompat-v7:23.0.1'
查看更多
Juvenile、少年°
7楼-- · 2019-01-11 22:43

I got the same issue when inflating XML containing TextInputLayout. The problem was fixed by setting the correct Style on my application. Just like it says here : android design support library

I've the following issue

Caused by: android.view.InflateException: Binary XML file line #38: Error inflating class android.support.design.widget.TextInputLayout

My style.xml was

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="AppTheme" parent="android:Theme.Material">
        <!-- Customize your theme here. -->
    </style>
</resources>

As it said in this post on Design Support Library

Note that as the Design library depends on the Support v4 and AppCompat Support Libraries, those will be included automatically when you add the Design library dependency.

So NO NEED TO ADD the following line inside the gradle file

compile 'com.android.support:appcompat-v7:22.2.0'

I found the link behove explaining that the Design Support Library is part of the AppCompat and it require the AppCompat Theme base to work. So I've modify my style.xml to be

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
    </style>
</resources>

And it worked.

查看更多
登录 后发表回答