关于 Android:Android – 无法实例化一个或多个类
Android - Failed to instantiate one or more classes
我目前正在开发 Android UI,并遇到以下问题。

基于 android.support.design.widget.AppBarLayout 注释,我假设这是 Gradle 的问题。我也无法解决以下问题。

下面是gradle文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
apply plugin: 'com.android.application' android { compileSdkVersion 28 defaultConfig { applicationId"shapetheworld.application.shapetheworld" minSdkVersion 18 targetSdkVersion 28 versionCode 1 versionName"1.0" testInstrumentationRunner"android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } repositories { maven { url 'https://jitpack.io' } } dependencies { implementation fileTree(include: ['*.jar'], dir: 'libs') implementation 'com.android.support:appcompat-v7:28.0.0-rc01' implementation 'com.android.support:design:27.1.0' implementation 'com.android.support.constraint:constraint-layout:1.1.2' implementation 'com.github.PhilJay:MPAndroidChart:v3.0.3' 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' } |
下面是activity_main.xml文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<?xml version="1.0" encoding="utf-8"?> <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:id="@+id/coordinatorLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" tools:context="shapetheworld.application.shapetheworld.MainActivity" tools:layout_editor_absoluteY="25dp"> <android.support.design.widget.AppBarLayout android:id="@+id/appBarLayout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:theme="@style/AppTheme.AppBarOverlay"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" /> </android.support.design.widget.AppBarLayout> <include layout="@layout/content_main" /> </android.support.constraint.ConstraintLayout> |
注意:如果有关系,我使用的是 Android Studio 3.1.4
还有一些参考资料表明问题可能出在 style.xml 中,因此可以在下面找到文件。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<resources> <!-- Base application theme. --> <style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style> <style name="AppTheme.NoActionBar"> <item name="windowActionBar">false</item> <item name="windowNoTitle">true</item> </style> <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" /> <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" /> </resources> |
参考文献:
- 无法实例化以下类: - android.support.v7.widget.Toolbar
- 找不到以下类: - android.support.design.widget.TextInputLayout
- Android 自定义操作栏示例教程
- Android 无法实例化一个或多个类
相关讨论
- 所有支持库都应该具有相同的版本尝试使用 com.android.support:appcompat-v7:27.1.0
- @ShivamPokhriyal,感谢您的提示,您的建议加上将 compileSdkVersion 和 targetSdkVersion 更改为 27 有效。将此作为解决方案发布,以便我接受答案
您使用的所有支持库都需要具有相同的版本。
现在,您可以替换此行
1 |
implementation 'com.android.support:appcompat-v7:28.0.0-rc01' |
与
1 |
implementation 'com.android.support:appcompat-v7:27.1.0' |
还将您的 compileSdkVersion 和 targetSdkVersion 版本更改为 27。