android.support.design.widget.TextInputLayout 无法实例化

android.support.design.widget.TextInputLayout could not be instantiated

我想将 android.support.design.widget.TextInputLayout 集成到我的 android 应用程序中。我已将 jar 文件 android-support-design.jar 从 sdk 复制到我的应用程序 lib 文件夹。我在我的 xml 文件中为电子邮件 EditText 添加了以下代码,

 <!-- Email Label -->
        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp">
            <EditText android:id="@+id/input_email"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="textEmailAddress"
                android:hint="Email" />
        </android.support.design.widget.TextInputLayout>

在我的 layout.xml 文件中添加此代码时,我收到类似

的错误
Exception raised during rendering: com.android.layoutlib.bridge.MockView cannot be cast to android.view.ViewGroup
Exception details are logged in Window > Show View > Error Log
The following classes could not be instantiated:
- android.support.design.widget.TextInputLayout (Open Class, Show Error Log)
See the Error Log (Window > Show View) for more details.

我该如何解决这个问题...

如果您使用 AndroidStudio,则不应包含 android-support-design.jar。 相反,在 build.gradle:

中像下面这样写
dependencies {
    ...
    compile 'com.android.support:design:24.0.0'
    ...
}

编辑:如果这不起作用,您可能使用了不同的版本。在 Windows 中,转到:

[android-sdk]\extras\android\m2repository\com\android\support\design

在 Mac:

sdk/extras/android/m2repository/com/android/support/design

此目录包含多个版本文件夹。在 build.gradle.

上使用最新版本

您提供的 id 有误 将 id 赋予 TextInputLayout

替换

android.support.design.widget.TextInputLayout

com.google.android.material.textfield.TextInputLayout

我遇到了类似的问题,将 TextInputLayout 设计模式替换为以下为我解决的问题:

com.google.android.material.textfield.TextInputLayout

在build.gradle这一行之后添加:

implementation 'com.android.support:design:28.0.0'

并在 xml 文件中使用:

 <com.google.android.material.textfield.TextInputLayout
                    android:id="@+id/inputLayoutMobile"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="@dimen/dimen_20dp"
                    android:layout_marginRight="@dimen/dimen_20dp"
                    android:hint="@string/phone_number"
                    android:visibility="gone"
                    android.support.design:layout_constraintTop_toBottomOf="@+id/inputLayoutAppServerUrl">

                    <com.google.android.material.textfield.TextInputEditText
                        android:id="@+id/edtTxtMobile"
                        style="@style/Style_TextView"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:drawableLeft="@drawable/ic_smartphone"
                        android:drawablePadding="@dimen/dimen_10dp"
                        android:inputType="number"
                        android:singleLine="true"
                        android:scrollbars="vertical"
                        android:text="@={SettingViewModel.mMobileNo}"
                        android:textSize="@dimen/font_13sp" />
                </com.google.android.material.textfield.TextInputLayout>

您使用的依赖项是 Android 支持库的一部分,该库现已弃用并且 Google 不再支持。相反,使用这个:
implementation 'com.google.android.material:material:<version>'
并将 <version> 替换为 Google's Maven Repository or MVN Repository

中的最新版本

也许你可以从这里阅读更多内容Getting Started

对于 android androidX 添加此依赖项

implementation 'com.google.android.material:material:1.2.0'

那就试试

如果实现、编译和 api 不起作用,请尝试将类路径与最新版本的 material 库一起使用,您可以在 Google's Maven Repository

上找到最新版本
dependencies {
    .....
    classpath 'com.google.android.material:material:1.4.0-beta01'
    ....
}