由于TextInputLayout而导致布局膨胀时出错

时间:2016-03-18 13:27:18

标签: android xml layout android-appcompat android-textinputlayout

我在给布局膨胀时遇到这样的错误:

Failed to inflate
android.view.InflateException: Binary XML file line #36: Error inflating class <unknown>
at android.view.LayoutInflater.createView(LayoutInflater.java:613)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)

在活动文件中:

    ViewGroup viewGroup = (ViewGroup) findViewById(R.id.education_details);
    LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.content_contact,null);

有些答案表明由于样式而产生错误。所以这是我的style.xml:

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

然后在AndroidManifest.xml中:

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

布局文件(content_contact.xml):

  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <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="@string/email"
            android:singleLine="true"
            android:textAllCaps="true"
            android:inputType="textEmailAddress" />
    </android.support.design.widget.TextInputLayout>
   </LinearLayout>

Gradle中的依赖关系:

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
compile 'com.google.android.gms:play-services-ads:8.4.0'
}

如果我不使用TextInputLayout,那么它可以完美运行。 我尝试了几个答案,但没有一个适合我。 如何解决这个错误?

2 个答案:

答案 0 :(得分:0)

删除此代码

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

并添加此代码

 LayoutInflater inflater = LayoutInflater.from(this);

答案 1 :(得分:0)

问题是您的“ AppTheme”未设置为展开视图。您可以通过使用ContextThemeWrapper初始化LayoutInflater来解决此问题:

LayoutInflater inflater = LayoutInflater.from(new ContextThemeWrapper(getApplicationContext(), R.style.AppTheme));