在 Android 4.4.2 上创建 TextInputLayout 时膨胀类 EditText 时出错:

Posted

技术标签:

【中文标题】在 Android 4.4.2 上创建 TextInputLayout 时膨胀类 EditText 时出错:【英文标题】:Error inflating class EditText on creating TextInputLayout on Android 4.4.2: 【发布时间】:2016-07-22 22:56:28 【问题描述】:

我在应用程序的登录页面中使用了 TextInputLayout UI 机制,除了在此设备上运行 4.4.2 android 版本的设备之外,一切都很好。我有一个例外。

这是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_>

<include android:id="@+id/loginInfiLogoRL"
    layout="@layout/login_infi_logo_layout"
    android:layout_
    android:layout_
    android:paddingLeft="-3dp"
    android:layout_marginTop="5dp"
    android:layout_marginBottom="10dp"/>

<!--EMAIL-->
<android.support.design.widget.TextInputLayout
    android:id="@+id/email_wrapper"
    android:layout_
    android:layout_
    android:layout_below="@+id/loginInfiLogoRL"
    android:theme="@style/TextLabel">

    <EditText
        android:id="@+id/etUserName"
        android:layout_
        android:layout_
        android:hint="@string/login_screen_email_hint"
        android:imeOptions="actionNext"
        android:inputType="textEmailAddress"
        android:maxLines="1"
        android:nextFocusDown="@+id/etPassword"
        android:singleLine="true"
        android:textColor="@android:color/white"
        android:textColorHighlight="@color/Orange"
        android:textColorHint="@android:color/white" />

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


<RelativeLayout
    android:id="@+id/rlPassword"
    android:layout_
    android:layout_
    android:layout_marginTop="12dp"
    android:layout_below="@id/email_wrapper">

    <!--PASSWORD-->
    <android.support.design.widget.TextInputLayout
        android:id="@+id/password_wrapper"
        android:layout_
        android:layout_
        android:theme="@style/TextLabel">

        <EditText
            android:id="@+id/etPassword"
            android:layout_
            android:layout_
            android:hint="@string/login_screen_password_hint"
            android:imeOptions="actionNext"
            android:inputType="textPassword"
            android:maxLines="1"
            android:nextFocusDown="@+id/bSignIn"
            android:shadowColor="@color/Orange"
            android:singleLine="true"
            android:textColor="@android:color/white"
            android:textColorHint="@android:color/white" />

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

    <!--FORGOT-->
    <TextView
        android:id="@+id/tvForgotPassword"
        android:layout_
        android:layout_
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_centerInParent="true"
        android:paddingBottom="14dp"
        android:paddingRight="10dp"
        android:text="@string/login_screen_forgot_password_text"
        android:textColor="@color/Orange"
        android:textSize="16sp" />

</RelativeLayout>

<TextView
    android:id="@+id/tvRememberMe"
    android:layout_
    android:layout_
    android:visibility="invisible" />


<!--Buttons Login and SignUp-->
<LinearLayout
    android:id="@+id/llSignUp"
    android:layout_
    android:layout_
    android:layout_marginLeft="15dp"
    android:layout_marginRight="15dp"
    android:layout_marginTop="10dp"
    android:gravity="center_vertical"
    android:layout_below="@+id/rlPassword">

    <!--Sign up Button-->
    <TextView
        android:id="@+id/bSignUp"
        android:layout_
        android:layout_
        android:layout_marginRight="16dp"
        android:layout_weight="1"
        android:textSize="18sp"
        android:gravity="center"
        android:imeOptions="actionGo"
        android:text="@string/login_screen_sign_up_button_text"
        android:textColor="@color/Orange" />

    <!--Login Button-->
    <Button
        android:id="@+id/bSignIn"
        android:layout_
        android:layout_
        android:layout_marginLeft="16dp"
        android:layout_weight="1"
        android:background="@drawable/fragment_first_use_cloud_get_started_button_background"
        android:focusable="true"
        android:imeOptions="actionGo"
        android:text="@string/login_screen_sign_in_button_text"
        android:textColor="@color/Orange" />

</LinearLayout>

<LinearLayout
    android:id="@+id/llFacebook"
    android:layout_
    android:layout_
    android:layout_marginTop="15dp"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/llSignUp"
    android:gravity="right">

    <TextView
        android:layout_
        android:layout_
        android:layout_gravity="center_vertical"
        android:layout_marginRight="8dp"
        android:gravity="center"
        android:text="@string/log_in_with_facebook"
        android:textAppearance="@style/TextAppearance.AppCompat.Small.Inverse"
        android:textColor="@android:color/white" />

    <com.facebook.login.widget.LoginButton
        android:id="@+id/bFacebookLogin"
        android:layout_
        android:layout_
        android:background="@drawable/btnfbconnect"
        android:text=""
        android:textColor="@android:color/white"/>

</LinearLayout>


<!--Privacy-->
<ImageView
    android:id="@+id/ivLockImage"
    android:layout_
    android:layout_
    android:layout_alignParentLeft="true"
    android:layout_below="@id/llFacebook"
    android:layout_marginTop="18dp"
    android:src="@drawable/iconshieldprivacy" />

<TextView
    android:id="@+id/textView2"
    android:layout_
    android:layout_
    android:layout_alignEnd="@+id/llSignUp"
    android:layout_alignParentRight="@+id/ivLockImage"
    android:layout_alignRight="@+id/llSignUp"
    android:layout_alignTop="@+id/ivLockImage"
    android:layout_toRightOf="@+id/ivLockImage"
    android:text="@string/login_screen_infi_privacy_title"
    android:textColor="@android:color/white"
    android:textSize="12sp"
    android:textStyle="bold" />

<TextView
    android:id="@+id/text1"
    android:layout_
    android:layout_
    android:layout_alignEnd="@+id/textView2"
    android:layout_alignRight="@+id/textView2"
    android:layout_below="@+id/textView2"
    android:layout_toRightOf="@+id/ivLockImage"
    android:paddingTop="3dp"
    android:text="@string/login_screen_infi_privacy_description"
    android:textColor="@android:color/white"
    android:textSize="10sp" />

<Space
    android:layout_
    android:layout_
    android:minHeight="50dp"
    android:layout_alignStart="@+id/text1"
    android:layout_alignLeft="@+id/text1"
    android:layout_below="@+id/text1"
    android:layout_alignParentBottom="true" />
</RelativeLayout>

更新:这是xml样式:

    <style name="TextLabel" parent="TextAppearance.AppCompat">
    <!-- Hint color and label color in FALSE state -->
    <item name="android:textColorHint">@android:color/white</item>
    <item name="android:textColorHighlight">@android:color/white</item>
    <item name="android:textColorLink">@android:color/white</item>
    <item name="android:textSize">20sp</item>
    <!-- Label color in TRUE state and bar color FALSE and TRUE State -->
    <item name="colorAccent">@color/accent</item>
    <item name="colorControlNormal">@android:color/white</item>
    <item name="colorControlActivated">@color/accent</item>
</style>

这是个例外:

Fatal Exception: java.lang.RuntimeException: Unable to start activity ComponentInfocom.infibond.infi/com.infibond.account.login.LoginActivity: android.view.InflateException: Binary XML file line #22: Error inflating class EditText
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2281)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2359)
   at android.app.ActivityThread.access$800(ActivityThread.java:139)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:136)
   at android.app.ActivityThread.main(ActivityThread.java:5230)
   at java.lang.reflect.Method.invokeNative(Method.java)
   at java.lang.reflect.Method.invoke(Method.java:515)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:780)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:596)
   at dalvik.system.NativeStart.main(NativeStart.java)
Caused by android.view.InflateException: Binary XML file line #22: Error inflating class EditText
       at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
       at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
       at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
       at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
       at uk.co.chrisjenx.calligraphy.CalligraphyLayoutInflater.inflate(CalligraphyLayoutInflater.java:60)
       at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
       at com.infibond.account.login.LoginFragment.onCreateView(LoginFragment.java:92)
       at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
       at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
       at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
       at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
       at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
       at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:330)
       at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:547)
       at com.infibond.base.Inheritance.Activities.ActivityBase.onStart(ActivityBase.java:112)
       at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
       at android.app.Activity.performStart(Activity.java:5421)
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2242)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2359)
       at android.app.ActivityThread.access$800(ActivityThread.java:139)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:136)
       at android.app.ActivityThread.main(ActivityThread.java:5230)
       at java.lang.reflect.Method.invokeNative(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:515)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:780)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:596)
       at dalvik.system.NativeStart.main(NativeStart.java)
Caused by java.lang.UnsupportedOperationException: Can't convert to color: type=0x2
       at android.content.res.TypedArray.getColor(TypedArray.java:327)
       at android.widget.TextView.<init>(TextView.java:703)
       at android.widget.EditText.<init>(EditText.java:61)
       at android.support.v7.widget.AppCompatEditText.<init>(AppCompatEditText.java:60)
       at android.support.v7.widget.AppCompatEditText.<init>(AppCompatEditText.java:56)
       at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:101)
       at android.support.v7.app.AppCompatDelegateImplV7.createView(AppCompatDelegateImplV7.java:938)
       at android.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:992)
       at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
       at uk.co.chrisjenx.calligraphy.CalligraphyLayoutInflater$WrapperFactory2.onCreateView(CalligraphyLayoutInflater.java:280)
       at android.view.LayoutInflater$FactoryMerger.onCreateView(LayoutInflater.java:172)
       at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:684)
       at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
       at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
       at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
       at uk.co.chrisjenx.calligraphy.CalligraphyLayoutInflater.inflate(CalligraphyLayoutInflater.java:60)
       at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
       at com.infibond.account.login.LoginFragment.onCreateView(LoginFragment.java:92)
       at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
       at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
       at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
       at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
       at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
       at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:330)
       at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:547)
       at com.infibond.base.Inheritance.Activities.ActivityBase.onStart(ActivityBase.java:112)
       at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
       at android.app.Activity.performStart(Activity.java:5421)
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2242)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2359)
       at android.app.ActivityThread.access$800(ActivityThread.java:139)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:136)
       at android.app.ActivityThread.main(ActivityThread.java:5230)
       at java.lang.reflect.Method.invokeNative(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:515)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:780)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:596)
       at dalvik.system.NativeStart.main(NativeStart.java)

为什么会发生这种情况,修复它的正确方法是什么?

【问题讨论】:

我可以看看你的Orange颜色值吗? @MohammadRahchamani 这里是:#F16725 好的。看我的回答。 检查这个:***.com/questions/9598349/… @EmilAdz 在 Textlabel 样式中将父级更改为“android:TextAppearance”并试一试 【参考方案1】:

解决此问题的方法是从样式配置中删除 parent="TextAppearance.AppCompat"

所以现在样式如下:

<style name="TextLabel">
    <!-- Hint color and label color in FALSE state -->
    <item name="android:textColorHint">@color/White</item>
    <item name="android:textColorHighlight">@android:color/white</item>
    <item name="android:textColorLink">@color/White</item>
    <item name="android:textSize">20sp</item>
    <!-- Label color in TRUE state and bar color FALSE and TRUE State -->
    <item name="colorAccent">@color/accent</item>
    <item name="colorControlNormal">@android:color/white</item>
    <item name="colorControlActivated">@color/accent</item>
</style>

在xml布局中:

<android.support.design.widget.TextInputLayout
    android:id="@+id/email_wrapper"
    android:layout_
    android:layout_
    android:layout_below="@+id/loginInfiLogoRL"
    android:theme="@style/TextLabel">

    <EditText
        android:id="@+id/etUserName"
        android:layout_
        android:layout_
        android:hint="@string/login_screen_email_hint"
        android:imeOptions="actionNext"
        android:inputType="textEmailAddress"
        android:maxLines="1"
        android:nextFocusDown="@+id/etPassword"
        android:singleLine="true"
        android:textColor="@android:color/white"
        android:textColorHighlight="@color/White"
        android:textColorHint="@android:color/white" />

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

【讨论】:

你知道parent="TextAppearance.AppCompat" 是什么导致了这次崩溃吗? @Phoenix 不,我不知道。【参考方案2】:

您的问题出在另一个 xml 文件上。我认为这是因为您在其中一个可绘制文件中使用了 color 属性。 阅读here了解更多信息。

【讨论】:

我不太明白,什么意思....那么这个问题应该怎么解决呢? 我可以看看你的@style/TextLabel 文件吗? 已添加到问题中。

以上是关于在 Android 4.4.2 上创建 TextInputLayout 时膨胀类 EditText 时出错:的主要内容,如果未能解决你的问题,请参考以下文章

让 SSLEngine 在 Android (4.4.2) 上使用 TLSv1.2?

eclipse没有创建Android文件夹

在 Android OS 4.4.2/MobileFirst 混合应用程序上启用 TLS 1.2

即使在 Android 4.4.2 中关闭了位置服务,GetCurrentLocation 仍在 Cordova/PhoneGap 应用程序上工作

Meteor Android 应用程序无法在 4.1.1 上安装

在 android 4.3+ 的 textview 中缺少一些字母和数字