[英]The following classes could not be instantiated: - android.support.v7.widget.Toolbar
[英]How to fix 'The following classes could not be found: - android.support.v7.widget.Toolbar' error in Java
我正在嘗試向我的應用程序添加一個工具欄,但是 xml 給出了這個錯誤
找不到以下類: - android.support.v7.widget.Toolbar(修復構建路徑、編輯 XML、創建類) 提示:嘗試構建項目。
提示:嘗試刷新布局。
我嘗試以這種方式連接庫庫,但沒有幫助
implementation 'com.android.support:appcompat-v7:28.0.0'
我還使用幫助 SDK Manager 下載了 Support Repository 並將 AndroidManifest.xml 中的應用程序樣式更改為
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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">
<android.support.v7.widget.Toolbar
android:id="@+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
</LinearLayout>
apply plugin: 'com.android.application'
android {
compileSdkVersion 29
buildToolsVersion "29.0.0"
defaultConfig {
applicationId "com.andrew.forideas"
minSdkVersion 21
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
allprojects {
repositories {
google()
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.andrew.forideas">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
在您的 MainActivity 下添加:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
在你的styles.xml 中添加這個主題!!!
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
在 xml 文件中:
android:theme="@style/AppTheme.AppBarOverlay"
有用就點個贊吧!
嘗試使用androidx
依賴項中的工具欄
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolBarTop"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="@color/colorPrimary"/>
// YOUR INNER CONTENT
</androidx.appcompat.widget.Toolbar>
在我的情況下 fontFamily 名稱不正確,我更改了它並且錯誤消失了
android:fontFamily="sans-serif-medium"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.