为什么赢了';t androidx是否支持导航栏?
因此,我试图使用本教程完成导航抽屉()然而,我了解到,自从使用AndroidX和工具栏等上传教程以来,Android Studio中发生了一些变化,但我不确定发生了什么变化 我已经试了几个小时来理解这个问题,但我是android studio的新手,所以我一无所知。应用程序一直在崩溃 基本上,我只是不知道如何导入工具栏来让它工作 这是相关的主要活动信息为什么赢了';t androidx是否支持导航栏?,android,android-studio,androidx,Android,Android Studio,Androidx,因此,我试图使用本教程完成导航抽屉()然而,我了解到,自从使用AndroidX和工具栏等上传教程以来,Android Studio中发生了一些变化,但我不确定发生了什么变化 我已经试了几个小时来理解这个问题,但我是android studio的新手,所以我一无所知。应用程序一直在崩溃 基本上,我只是不知道如何导入工具栏来让它工作 这是相关的主要活动信息 import androidx.appcompat.app.ActionBarDrawerToggle; import androidx.app
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
public class MainActivity extends AppCompatActivity {
private DrawerLayout drawer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
布局如下:
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar
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/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity"
tools:openDrawer="start">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
从父视图中删除androidx.appcompat.widget.Toolbar并使用以下xml
<androidx.drawerlayout.widget.DrawerLayout
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/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity"
tools:openDrawer="start">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</androidx.drawerlayout.widget.DrawerLayout>
错误表明您正在使用support compat的抽屉布局,您必须使用物料抽屉布局 下面是如何添加库 下面是如何添加抽屉
AndroidX使用AndroidX命名空间中的包替换了原始的支持库API 看起来您正在XML中使用一些支持库类,如日志中所示:
Didn't find class "android.support.v4.widget.DrawerLayout" on path: DexPathList
android.support.v4.widget.DrawerLayout
在MainActivity中正在膨胀的xml中,替换以下内容:
android.support.v4.widget.DrawerLayout
为此:
androidx.drawerlayout.widget.DrawerLayout
这将为您解决此错误
但是,我确信,使用较旧的支持库类将有更多的XML。如上所述,引用并搜索导致错误的类,并将其替换为相应的androidx类。发布build.gradle模块和崩溃日志ClassNotFoundException:
android.support.v4.widget.DrawerLayout
。将其更改为androidx.drawerlayout.widget.drawerlayout
。请尝试此选项
androidx.drawerlayout.widget.DrawerLayout