我在 TextInputLayout 中使用 TextInputEditText 并且从今天开始它运行良好。我刚刚更改了颜色的属性,它导致了这个错误:
android.view.InflateException: Binary XML file line #97: Binary XML file line #8: Error inflating class com.google.android.material.textfield.TextInputLayout
Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class com.google.android.material.textfield.TextInputLayout
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
at android.view.LayoutInflater.createView(LayoutInflater.java:647)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)
at android.view.LayoutInflater.parseInclude(LayoutInflater.java:965)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:859)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:866)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:866)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:866)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at androidx.databinding.DataBindingUtil.inflate(DataBindingUtil.java:126)
at androidx.databinding.DataBindingUtil.inflate(DataBindingUtil.java:95)
at com.project.utils.CustomResources$Companion.inflateLayout(CustomResources.kt:62)
at com.project.screens.Registration.getScreen(RegistrationScreen.kt:34)
at com.project.registration.CustomViewPagerAdapter.instantiateItem(CustomViewPagerAdapter.kt:40)
at com.project.registration.CustomViewPagerAdapter.instantiateItem(CustomViewPagerAdapter.kt:25)
at androidx.viewpager.widget.ViewPager.addNewItem(ViewPager.java:1010)
at androidx.viewpager.widget.ViewPager.populate(ViewPager.java:1224)
at androidx.viewpager.widget.ViewPager.populate(ViewPager.java:1092)
at androidx.viewpager.widget.ViewPager.onMeasure(ViewPager.java:1622)
at android.view.View.measure(View.java:23180)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
at android.view.View.measure(View.java:23180)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6764)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at androidx.appcompat.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:146)
at android.view.View.measure(View.java:23180)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6764)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1535)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:825)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:704)
at android.view.View.measure(View.java:23180)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6764)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:23180)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6764)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:23180)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6764)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at com.android.internal.policy.DecorView.onMeasure(DecorView.java:716)
at android.view.View.measure(View.java:23180)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2734)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1583)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1867)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1471)
2021-01-12 12:26:03.681 21736-21736/? E/AndroidRuntime: at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7215)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1012)
at android.view.Choreographer.doCallbacks(Choreographer.java:823)
at android.view.Choreographer.doFrame(Choreographer.java:758)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:998)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6735)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 1: TypedValue{t=0x2/d=0x7f030375 a=5}
at android.content.res.TypedArray.getColorStateList(TypedArray.java:546)
at androidx.appcompat.widget.TintTypedArray.getColorStateList(TintTypedArray.java:184)
at com.google.android.material.textfield.TextInputLayout.<init>(TextInputLayout.java:561)
at com.google.android.material.textfield.TextInputLayout.<init>(TextInputLayout.java:420)
... 64 more
我正在为我的 App 样式使用自定义属性,因此我添加了新属性并为该属性创建了相同的选择器,但颜色不同。
attrs.xml
<attr name="textInputFieldHint" format="reference"/>
样式.xml
<style name="AppThemeBlueColor" parent="Theme.MaterialComponents.Light.NoActionBar.Bridge">
...
<item name="textInputFieldHint">@color/textinputfield_hint_blue</item>
</style>
textinputfield_hint_blue.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/darkBlue" android:state_focused="true"/>
<item android:color="@color/darkGrey"/>
</selector>
文本输入编辑文本样式:
<style name="TextInputLayoutStyle" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<item name="android:textColor">@color/form_edit_text_color</item>
<item name="android:textColorHint">?attr/textInputFieldHint</item>
<item name="hintTextColor">?attr/textInputFieldHint</item>
<item name="boxStrokeColor">?attr/textInputFieldOutline</item>
<item name="errorTextColor">@color/colorRed</item>
<item name="android:textSize">@dimen/text_normal</item>
<item name="android:layout_marginBottom">@dimen/textInputLayout_margin_bottom</item>
</style>
<style name="TextInputEditTextStyle">
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">@color/form_edit_text_color</item>
<item name="android:textColorHint">?attr/textInputFieldHint</item>
<item name="boxStrokeColor">?attr/textColorTertiary</item>
<item name="errorTextColor">@color/colorRed</item>
<item name="android:textSize">@dimen/text_normal</item>
<item name="android:paddingStart">@dimen/textInputEditText_padding_start</item>
<item name="android:singleLine">true</item>
<item name="android:focusable">true</item>
<item name="android:focusableInTouchMode">true</item>
<item name="android:textCursorDrawable">@null</item>
</style>
我只是用textInputFieldOutline
属性替换了textInputFieldHint
属性。两者都是相同的选择器,但颜色不同。颜色确实存在,因为它用于其他布局组件。看起来像一些 Android 错误,因为当它的代码完全相同但颜色不同时,它没有任何理由不工作。