Android文本输入字段膨胀器错误

29

尝试在Android appcompat库中使用新的TextInputField时,我的应用程序崩溃了。以下是我的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>

我收到的错误消息:

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

解决方案:在您的TextInputLayout中添加hintTextAppearance属性,使前导标签如下所示:

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

你的解决方案有效了,谢谢! - temirbek
13个回答

43

请确保在您的gradle文件中具有以下依赖项:

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

工作示例:

<android.support.design.widget.TextInputLayout
    android:id="@+id/txtEmail_InpLyt"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textEmailAddress"
        android:ems="10"
        android:id="@+id/txtEmail"
        android:hint="Email Address"
        android:singleLine="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"/>
</android.support.design.widget.TextInputLayout>

(设置hintTextAppearance并不是必要的。)

更新:

如果您在较新版的Android(Marshmallow / Nougat)中遇到提示文本未显示的问题,请将库更新至版本22.2.1(请参见TextInputLayout not showing EditText hint before user focus on it)。

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

这个可以工作,但我真的很惊讶,为了使TextInputLayout正常工作,你必须添加另一个库。看起来做得不太好。 - eimmer
编译 'com.android.support:appcompat-v7:25.0.1' 编译 'com.android.support:design:25.0.1' } - Gil Snovsky
@GilSnovsky,我已经更新了我的库,但是我的一些应用用户(Android API级别23)仍然遇到这个令人沮丧的错误:“Caused by android.view.InflateException: Binary XML file line #17: Binary XML file line #2: Error inflating class android.support.design.widget.TextInputLayout”。 - Edijae Crusar
1
它对我有效,但你可能需要执行以下操作:Build -> Clean Project,然后重新构建/运行。 - Ibrahim.H

24

我也遇到了这个问题,并想出了一个解决方案,它不需要更改应用程序主题,而只是改变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>

如果您尚未添加appCompat库,则需要添加:

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

19

在AndroidX中,它对我有用,我有这些库。

 implementation 'com.android.support:appcompat-v7:28.0.0'
    //design widget
    implementation 'com.android.support:design:28.0.0'

我改变

<android.support.design.widget.TextInputLayout
for
<com.google.android.material.textfield.TextInputLayout

很好。伙计们,这是在今年这个时候的正确答案,因为它一直崩溃的原因是支持库已经从Android更改为AndroidX。 - Yosidroid
同时,您可以将支持设计依赖项更改为:implementation 'com.google.android.material:material:1.3.0-alpha02',现在已支持AndroidX。 - Aqua Freshka

4

当我解析包含TextInputLayout的XML时,遇到了同样的问题。通过在应用程序中设置正确的样式,问题得以解决。就像这里所说的一样:android design support library

我遇到了以下问题

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

我的style.xml文件如下:

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

正如在Design Support Library的这篇文章中所说

请注意,由于Design库依赖于Support v4和AppCompat支持库,因此在添加Design库依赖项时,它们将自动包含。

因此,不需要在gradle文件中添加以下行。
compile 'com.android.support:appcompat-v7:22.2.0'

我发现有必要解释一下设计支持库是AppCompat的一部分,并且需要使用AppCompat主题基础来工作。因此,我修改了我的style.xml文件为:

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

它起作用了。


不清楚你所链接的问题中哪一部分是“设置正确的样式”。你能否展示一个快速示例,比如一行代码/配置,说明你的意思?(总之最好还是概括一下链接内容,以防目标发生变化 :)) - kdbanman
可能会奏效,但如果您添加parent="android:Theme.Material"并且必须将API更改为21,那么使用支持库的用途是什么呢?@pierre - Shubham AgaRwal

2

为了清楚起见,在Android Studio 3.*版本中,这已经发生了变化,现在变成了

最初的回答:

implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support:design:27.1.1'

"最初的回答":如果添加后字体变成了红色波浪线,请接受Android Studio建议的版本。

下面的内容必须放在内部。
 dependencies {}

build.gradle(Module:app)的部分更改为以下内容:

最初的回答

dependencies {
    ...
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support:design:27.1.1'
}

then hit sync now


1

请确保LayoutInflater是从AppCompatActivity创建的

例如:

而不是

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

使用:
inflater = LayoutInflater.from(<appCompatActivity>);

1
使用以下内容:
View view = LayoutInflator.from(getActivity()).inflate(R.layout.your_layout_file,parent,false);

1

对我来说,这些解决方案都没有起作用。我正在使用

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

尝试了所有的解决办法。我的应用在低版本sdk的设备上运行得非常完美。但是在新的(大于sdk 26)设备上崩溃了。但是经过思考两天,最终找到了解决方法。
我的布局编辑器显示了以下错误: 以下类无法实例化: - android.support.design.widget.TextInputLayout enter image description here 但它没有给我足够的信息来处理。直到我查看了布局编辑器中的其他错误,我发现了渲染问题。
Couldn't resolve resource @string/path_password_strike_through

enter image description here

在调查这个渲染问题后,我终于找到了解决方案。你需要做的就是添加

app:passwordToggleDrawable="@string/string_name"

在你的TextInputLayout xml文件中。

                <android.support.design.widget.TextInputLayout
                    android:id="@+id/login_input_layout_password"
                    android:layout_width="match_parent"
                    app:passwordToggleDrawable="@drawable/ic_lock_outline_grey600_24dp"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="30dp"
                    android:layout_marginRight="30dp"
                    android:layout_marginTop="10dp"
                    android:backgroundTint="@color/colorAccent"
                    app:boxStrokeColor="#555"
                    android:textColorHint="@color/colorPrimary">

                    <EditText
                        android:id="@+id/login_input_password"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        />

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

1
最终我解决了这个问题...根据库本身的最新更改,将库更改为
<android.support.design.widget.TextInputLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/txt_input"
        android:layout_margin="@dimen/activity_horizontal_margin"
        app:layout_constraintTop_toBottomOf="@+id/edt_first_name"
        app:layout_constraintStart_toEndOf="@+id/guideline"
        app:hintEnabled="false">

        <android.support.design.widget.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Floating Hint Disabled" />

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

<com.google.android.material.textfield.TextInputLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/txt_input"
        android:layout_marginTop="@dimen/activity_horizontal_margin"
        app:layout_constraintTop_toBottomOf="@+id/edt_first_name"
        app:layout_constraintStart_toEndOf="@+id/guideline"
        app:hintEnabled="false">

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Floating Hint Disabled" />

    </com.google.android.material.textfield.TextInputLayout>

0

对我来说,将EditText更改为TextInputEditText

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

    <android.support.design.widget.TextInputEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/date_raffle"/>

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接