gra*_*sdy 14 android android-edittext android-textinputlayout android-textinputedittext
我正在尝试使用Material Design中的TextInputEditText(https://github.com/material-components/material-components-android/blob/master/docs/components/TextInputLayout.md),我得到了运行时异常.
这是运行日志的一部分:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.grigori.materialtextedit, PID: 12036
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.grigori.materialtextedit/com.example.grigori.materialtextedit.MainActivity}: android.view.InflateException: Binary XML file line #9: Binary XML file line #9: Error inflating class com.google.android.material.textfield.TextInputLayout
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
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: android.view.InflateException: Binary XML file line #9: Binary XML file line #9: Error inflating class com.google.android.material.textfield.TextInputLayout
Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class com.google.android.material.textfield.TextInputLayout
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.material.textfield.TextInputLayout" on path: DexPathList
Run Code Online (Sandbox Code Playgroud)
我通过包含许多apk文件路径的DexPathList剪切此日志,例如:
zip file "/data/app/com.example.grigori.materialtextedit-jamgTcrgG9neBKIMcqDo7Q==/base.apk"
Run Code Online (Sandbox Code Playgroud)
我的xml文件:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/hint_text"/>
</com.google.android.material.textfield.TextInputLayout>
Run Code Online (Sandbox Code Playgroud)
我的build.gradle依赖:
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0-rc01'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'}
Run Code Online (Sandbox Code Playgroud)
Md.*_*lah 32
通过继承以下其中一项来更新@style 文件夹中的主题:
Theme.MaterialComponents
Theme.MaterialComponents.NoActionBar
Theme.MaterialComponents.Light
Theme.MaterialComponents.Light.NoActionBar
Theme.MaterialComponents.Light.DarkActionBar
Run Code Online (Sandbox Code Playgroud)
像这样:
<style name="AppTheme.NoActionBar" parent="Theme.MaterialComponents.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimaryCustom</item>
<item name="colorPrimaryDark">@color/colorPrimaryDarkCustom</item>
<item name="colorAccent">@color/colorAccentCustom</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
Run Code Online (Sandbox Code Playgroud)
Ram*_*yer 23
这可能只是一个主题问题。如果您的 TextInputLayout 的父级是 MaterialComponents(如下所述)
<style name="TextInputLayout" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
...
</style>
Run Code Online (Sandbox Code Playgroud)
如果您的活动(或应用程序的)主题派生自 AppCompat,您的应用程序将崩溃,因为 MaterialComponents 和 AppCompat 主题不兼容。例如,AppTheme(对于活动或应用程序)不能如下:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
Run Code Online (Sandbox Code Playgroud)
您还必须将 AppTheme 设置为 MaterialComponents,如下所示:
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
Run Code Online (Sandbox Code Playgroud)
希望这可能会奏效。
Mih*_*ala 20
在我现有的项目中实施AndroidX时遇到了这个问题。
implementation 'com.google.android.material:material:1.0.0-beta01'
Run Code Online (Sandbox Code Playgroud)
布局XML
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/userNameWrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/TextLabel">
Run Code Online (Sandbox Code Playgroud)
老款式
<style name="TextLabel" parent="TextAppearance.AppCompat">
<item name="android:textColorHint">@color/hint_color</item>
<item name="android:textSize">@dimen/text_20sp</item>
<item name="colorControlNormal">@color/primaryGray</item>
<item name="colorControlActivated">@color/colorPrimary</item>
</style>
Run Code Online (Sandbox Code Playgroud)
新风格
<style name="TextLabel" parent="Widget.MaterialComponents.TextInputLayout.FilledBox">
<item name="android:textColorHint">@color/hint_color</item>
<item name="android:textSize">@dimen/text_20sp</item>
<item name="colorControlNormal">@color/primaryGray</item>
<item name="colorControlActivated">@color/colorPrimary</item>
</style>
Run Code Online (Sandbox Code Playgroud)
gra*_*sdy 12
感谢您的回答。问题是我用过com.google.android.material.textfield.TextInputLayout
。如果使用此控件,则应添加依赖项:
implementation 'com.google.android.material:material:1.0.0-beta01'
就我而言,我将xml重写为android.support.design.widget.TextInputLayout
:
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="@color/colorPrimary"
app:errorEnabled="true">
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/colorPrimary"
android:textColorHint="@color/colorPrimary"
android:hint="Username" />
</android.support.design.widget.TextInputLayout>
Run Code Online (Sandbox Code Playgroud)
另外,我还添加了依赖项:
implementation 'com.android.support:design:27.1.1'
这解决了我的问题。
Shu*_*pta 11
我在 AndroidX 中也遇到了同样的错误。我有一个对我有用的解决方案。
不要使用Theme.AppCompat
,而是将Theme.MaterialComponents
样式用作 AppTheme 或用于活动。
样式文件
<style name="AppTheme" parent="Theme.MaterialComponents.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>
Run Code Online (Sandbox Code Playgroud)
使用谷歌材料库。
implementation 'com.google.android.material:material:1.2.0'
Run Code Online (Sandbox Code Playgroud)
为什么会出现这个问题?
我注意到您正在为您使用自定义样式 TextInputLayout : style="@style/TextInputLayout"
这应该是@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox
或类似的。所以它需要Theme.MaterialComponents
作为活动的父主题。
更改您的应用主题以从 Material Components 主题继承
<style name="Theme.MyApp" parent="Theme.MaterialComponents.DayNight">
<!-- ... -->
</style>
Run Code Online (Sandbox Code Playgroud)
实际上,大多数人对AppTheme 的说法并不正确!
If you're using androidx
and do NOT want to have an MaterialComponents AppTheme
<!--style name="AppTheme" parent="Theme.MaterialComponents..."-->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
Run Code Online (Sandbox Code Playgroud)
You can always do the following and add the MaterialComponents Theme only to the parent container:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:theme="@style/Theme.MaterialComponents">
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/performing_attr1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Label"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"/>
</com.google.android.material.textfield.TextInputLayout>
Run Code Online (Sandbox Code Playgroud)
在应用程序 Gradle 中添加此依赖项
\n\nimplementation \'com.google.android.material:material:1.1.0-alpha09\'\n
Run Code Online (Sandbox Code Playgroud)\n\n在您的 TextInputLayout 的 style.xml 中添加此样式
\n\n<style name="TextLabel" parent="Widget.MaterialComponents.TextInputLayout.FilledBox">\n <item name="android:textColorHint">@color/colorGray600</item>\n <item name="android:textSize">@dimen/text_20sp</item>\n <item name="colorControlNormal">@color/colorGray900</item>\n <item name="colorControlActivated">@color/colorPrimary</item>\n</style>\n
Run Code Online (Sandbox Code Playgroud)\n\n像这样将其添加到您的 TextInputLayout 中
\n\nandroid:theme="@style/TextLabel"\n
Run Code Online (Sandbox Code Playgroud)\n\n并确保您使用了正确的主题作为父主题,如下所示
\n\nTheme.MaterialComponents.Light\n
Run Code Online (Sandbox Code Playgroud)\n\n现在这将解决 InflateException 问题。\n但是如果您有矢量可绘制对象,请按照以下步骤操作
\n\n1.您需要在 app\xe2\x80\x99s build.gradle 中选择加入 AndroidX 矢量支持:
\n\nandroid {\n...\ndefaultConfig {\n ...\n vectorDrawables.useSupportLibrary = true\n} }\n
Run Code Online (Sandbox Code Playgroud)\n\n2.如果您想以声明方式设置可绘制对象(即在您的布局中),则 appcompat 提供了许多您应该使用的 *Compat 属性,而不是标准平台的属性:
\n\nImageView, ImageButton:\n
Run Code Online (Sandbox Code Playgroud)\n\n唐\xe2\x80\x99t: android:src
\n执行:app:srcCompat
CheckBox, RadioButton:\n
Run Code Online (Sandbox Code Playgroud)\n\nDon\xe2\x80\x99t: android:button
\nDo: app:buttonCompat
\n TextView
(截至appcompat:1.1.0
):\nDon\xe2\x80\x99t:android:drawableStart android:drawableTop
等等。\nDo:app:drawableStartCompat app:drawableTopCompat
等等。
由于这些属性是 appcompat 库的一部分,因此请务必使用 app: 命名空间。在内部,这些 AppCompat* 视图使用 AppCompatResources 本身来启用加载向量。
\n\n3.如果你想使用内部代码那么
\n\nval drawable = AppCompatResources.getDrawable(context, R.drawable.my_vector)\n
Run Code Online (Sandbox Code Playgroud)\n\n**4.**如果您使用数据绑定,则可以使用自定义绑定适配器来完成:
\n\n/* Copyright 2018 Google LLC.\nSPDX-License-Identifier: Apache-2.0 */\n@BindingAdapter("indeterminateDrawableCompat")\nfun bindIndeterminateProgress(progressBar: ProgressBar, @DrawableRes id: Int) {\n val drawable = AppCompatResources.getDrawable(progressBar.context, id)\n progressBar.indeterminateDrawable = drawable\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n希望这能解决这个问题。
\n这个致命错误的主要原因是在设计文本输入布局时,非常需要更改应用程序的主题,或者不更改应用程序,然后为特定屏幕创建另一种样式。这可以通过以下方式完成
转到值,然后打开 styles.xml
这通常是你的应用程序主题的样子
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
Run Code Online (Sandbox Code Playgroud)
这里要改变的主要是
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
Run Code Online (Sandbox Code Playgroud)
将 appcompat 更改为材料组件
如果您不想更改应用程序主题
<style name="MaterialAppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
Run Code Online (Sandbox Code Playgroud)
我认为这会很好地工作,并帮助您扩展轮廓框或输入字段中的任何特定主题。
你应该添加
implementation 'com.github.material-components:material-components-android:1.0.0-rc01'
Run Code Online (Sandbox Code Playgroud)
到依赖项部分中的 build.gradle 应用程序级别,因此它看起来像:
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0-rc01'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.github.material-components:material-components-android:1.0.0-rc01'
}
Run Code Online (Sandbox Code Playgroud)