Как исправить ошибку «Следующие классы не найдены: - android.support.v7.widget.Toolbar» в Java - PullRequest
0 голосов
/ 08 июля 2019

Я пытаюсь добавить панель инструментов в свое приложение, но xml выдает эту ошибку

Не удалось найти следующие классы: - android.support.v7.widget.Toolbar (Fix BuildПуть, Редактировать XML, Создать класс) Совет: попробуйте создать проект.
Совет: Попробуйте обновить макет.

Я попытался подключить библиотеку библиотеки таким способом, но это не удалосьне помогает

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

Я также загрузил репозиторий поддержки с помощью SDK Manager и изменил стиль приложения в 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>

Ответы [ 2 ]

0 голосов
/ 08 июля 2019

Попробуйте использовать панель инструментов из 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>
0 голосов
/ 08 июля 2019

Добавьте это под своей 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"

Upvote, если полезно!

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...