【问题标题】:How to fix 'The following classes could not be found: - android.support.v7.widget.Toolbar' error in Java如何修复 Java 中的“找不到以下类:- android.support.v7.widget.Toolbar”错误
【发布时间】:2019-07-08 11:32:40
【问题描述】:

我正在尝试向我的应用程序添加工具栏,但 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">

activity_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)

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

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

【问题讨论】:

  • 将你的项目迁移到androidx中,如图Here,你混合了support libraryandroidx library

标签: java android xml


【解决方案1】:

尝试使用 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>

【讨论】:

    【解决方案2】:

    在你的 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"
    

    如果有用请点赞!

    【讨论】:

      【解决方案3】:

      在我的情况下 fontFamily 名称不正确,我更改它并且错误消失了

      android:fontFamily="sans-serif-medium"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-12-06
        • 2016-11-15
        • 1970-01-01
        • 2014-12-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多