【问题标题】:Android TextInputField Inflator ErrorAndroid TextInputField 充气机错误
【发布时间】:2015-08-15 05:39:45
【问题描述】:

尝试在 Android 上使用新的 TextInputField 时发生崩溃,想分享我的解决方案。

在 android appcompat 库中尝试新的 TextInputField 导致我的应用程序崩溃。这是我的布局 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.

解决方案: 将hintTextAppearance 属性添加到您的TextInputLayout 中,因此潜在客户标记如下所示:

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

【问题讨论】:

  • 您的解决方案有效,谢谢!

标签: android android-design-library


【解决方案1】:

确保您的 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 正常工作,你必须添加另一个库。看起来做得不好。
  • 编译 'com.android.support:appcompat-v7:25.0.1' 编译 'com.android.support:design:25.0.1' }
  • @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
  • 它对我有用,但是您可能需要:构建 -> 清理项目,然后重新构建/运行。
【解决方案2】:

这也发生在我身上,我想出了一个解决方案,不需要更改 App Theme,而只需更改 TextInputLayout 的 Theme:

<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'

【讨论】:

    【解决方案3】:

    在 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
    

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

    【讨论】:

    • 完美。伙计们,这是 b.c 一年中这个时候的正确答案,它不断崩溃的原因是 b.c 支持库从 android 更改为 androidx
    • 您还可以将支持设计依赖项更改为 :implementation 'com.google.android.material:material:1.3.0-alpha02' 现在支持 androidx
    【解决方案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>
    

    正如this post on Design Support Library中所说的那样

    请注意,由于设计库依赖于 Support v4 和 AppCompat 支持库,因此当您添加设计库依赖项时,它们会自动包含在内。

    所以不需要在 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>
    

    它奏效了。

    【讨论】:

    • 不清楚您链接的问题的哪一部分是“设置正确的样式”。你能用一个快速的例子来说明你的意思吗,比如一行代码/配置? (无论如何,最好总结一个链接,以防万一它的目标发生变化:)
    • -1 它可能有效,但如果添加 parent="android:Theme.Material" 则使用支持库有什么用处,您必须将 api 更改为 21。@pierre
    【解决方案5】:

    为了清楚起见,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'
    }
    

    然后点击sync now

    【讨论】:

      【解决方案6】:

      使用以下内容:

      View view = LayoutInflator.from(getActivity()).inflate(R.layout.your_layout_file,parent,false);
      

      【讨论】:

        【解决方案7】:

        确保 LayoutInflater 是从 AppCompatActivity 创建的

        例如。

        而不是

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

        使用:

        inflater = LayoutInflater.from(<appCompatActivity>);
        

        【讨论】:

          【解决方案8】:

          没有一个解决方案对我有用。我正在使用

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

          尝试了所有的解决方案。我的应用程序在 sdk 较低的设备上运行良好。但在较新的(大于 sdk 26)设备中崩溃。但是摸索了2天终于解决了。

          我的布局编辑器显示错误

          The following classes could not be instantiated: - android.support.design.widget.TextInputLayout

          但它没有给我足够的信息来处理。直到我在布局编辑器中查看了其他错误。在渲染问题下我发现

          Couldn't resolve resource @string/path_password_strike_through
          

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

          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>
          

          【讨论】:

            【解决方案9】:

            你可能已经添加了它 compile 'com.android.support:appcompat-v7:22.2.0' 但你需要添加compile 'com.android.support:design:22.2.0'

            你可以试试这个依赖:

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

            在你的 gradle 文件中。

            【讨论】:

              【解决方案10】:

              我在使用 Android Studio 3.0.1 时遇到了同样的问题

              您只需将以下行添加到 gradle.properties(项目属性)

              android.enableAapt2=false

              【讨论】:

              • #UPDATE :更改布局主题,其中定义了 TextInputField。示例:(在我的例子中,我在 activity_main.xml 中使用了 TextInputField 所以它会像: --> AppTheme 应该是 Theme.AppCompat 或 Decendent 的子节点
              【解决方案11】:

              对我来说,工作从 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>
              

              【讨论】:

                【解决方案12】:

                使用实现 'com.google.android.material:material:1.1.0' 你所要做的就是像这样使用你的 XML 标签:

                【讨论】:

                  【解决方案13】:

                  最后我得到了这个工作...根据库本身的最新更改,将库从

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

                  【讨论】:

                    猜你喜欢
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2016-07-15
                    • 1970-01-01
                    • 1970-01-01
                    • 2021-12-09
                    相关资源
                    最近更新 更多