简体   繁体   English

如何修复 Java 中的“找不到以下类:-android.support.v7.widget.Toolbar”错误

[英]How to fix 'The following classes could not be found: - android.support.v7.widget.Toolbar' error in Java

I'm trying to add a toolbar to my application, but xml gives this error我正在尝试向我的应用程序添加一个工具栏,但是 xml 给出了这个错误

The following classes could not be found: - android.support.v7.widget.Toolbar (Fix Build Path, Edit XML, Create Class) Tip: Try to build the project.找不到以下类: - android.support.v7.widget.Toolbar(修复构建路径、编辑 XML、创建类) 提示:尝试构建项目。
Tip: Try to refresh the layout.提示:尝试刷新布局。

I tried to connect the library library in this way, but it did not help我尝试以这种方式连接库库,但没有帮助

implementation 'com.android.support:appcompat-v7:28.0.0'

I also downloaded the Support Repository with the help SDK Manager and changed the style of the application in AndroidManifest.xml to我还使用帮助 SDK Manager 下载了 Support Repository 并将 AndroidManifest.xml 中的应用程序样式更改为

android:theme="@style/Theme.AppCompat.Light.NoActionBar">

activity_main.xml活动_main.xml

<?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>

build.gradle(Module.app) build.gradle(Module.app)

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'
}

AndroidManifest.xml AndroidManifest.xml

<?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>

Add this under your MainActivity:在您的 MainActivity 下添加:

@Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_home);
    Toolbar toolbar = findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);

Add this theme in your styles.xml!!!在你的styles.xml 中添加这个主题!!!

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

In xml file:在 xml 文件中:

 android:theme="@style/AppTheme.AppBarOverlay"

Upvote if useful!有用就点个赞吧!

Try to use the toolbar from the androidx dependencies尝试使用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.

相关问题 无法实例化以下类: - android.support.v7.widget.Toolbar - The following classes could not be instantiated: - android.support.v7.widget.Toolbar android.support.v7.widget.toolbar 在使用 android studio 的 java 文件中找不到? - android.support.v7.widget.toolbar is not found in java file using android studio? 使用android.support.v7.widget.Toolbar的NullPointerException - NullPointerException with android.support.v7.widget.Toolbar 错误膨胀android.support.v7.widget.Toolbar - error inflating android.support.v7.widget.Toolbar 找不到以下类: - android.support.v7.widget.RecyclerView - The following classes could not be found: - android.support.v7.widget.RecyclerView 错误:升级Android SDK工具后,“找不到以下类:-android.support.v7.widget.GridLayout” - Error: “The following classes could not be found: - android.support.v7.widget.GridLayout” after upgrading Android SDK Tools java.lang.ClassCastException:android.widget.LinearLayout无法强制转换为android.support.v7.widget.Toolbar - java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.support.v7.widget.Toolbar 即使导入android.support.v7.widget.Toolbar后,setSupportActionBar(toolbar)运行时错误 - setSupportActionBar(toolbar) runtime Error even after importing android.support.v7.widget.Toolbar 解组时找不到类:android.support.v7.widget.Toolbar$SavedState - Class not found when unmarshalling: android.support.v7.widget.Toolbar$SavedState RelativeLayout无法强制转换为android.support.v7.widget.Toolbar - RelativeLayout cannot be cast to android.support.v7.widget.Toolbar
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM