我不知道为什么工具栏导入不适用于Android Studio

时间:2019-11-09 13:34:38

标签: android android-studio toolbar

因此,我尝试使用本教程(https://www.youtube.com/watch?v=fGcMLu1GJEc&t=16s)完成导航抽屉操作,但是我很不高兴,自从教程使用AndroidX和工具栏及其他内容上传以来,Android Studio发生了一些变化,但是我我不确定发生了什么变化。

我已经尝试了几个小时才能理解问题,但是我是android studio的新手,所以我一无所知。该应用程序一直崩溃

基本上,我只是不知道要使用什么工具栏才能使其工作

这是与之相关的主要活动信息

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>

这是logcat错误

2019-11-09 13:53:58.560 5972-5972/? I/ample.fyptrial: Not late-enabling -Xcheck:jni (already on)
2019-11-09 13:53:58.614 5972-5972/? W/ample.fyptrial: Unexpected CPU variant for X86 using defaults: x86
2019-11-09 13:53:59.891 5972-5972/com.example.fyptrial1 W/ample.fyptrial: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (light greylist, reflection)
2019-11-09 13:53:59.893 5972-5972/com.example.fyptrial1 W/ample.fyptrial: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (light greylist, reflection)
2019-11-09 13:53:59.899 5972-5972/com.example.fyptrial1 D/AndroidRuntime: Shutting down VM
2019-11-09 13:53:59.922 5972-5972/com.example.fyptrial1 E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.fyptrial1, PID: 5972
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fyptrial1/com.example.fyptrial1.MainActivity}: android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class android.support.v4.widget.DrawerLayout
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
     Caused by: android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class android.support.v4.widget.DrawerLayout
     Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class android.support.v4.widget.DrawerLayout
     Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.v4.widget.DrawerLayout" on path: DexPathList[[zip file "/data/app/com.example.fyptrial1-QrFsh8JGp3aVPMyDmmBcJA==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.fyptrial1-QrFsh8JGp3aVPMyDmmBcJA==/lib/x86, /system/lib]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
        at android.view.LayoutInflater.createView(LayoutInflater.java:606)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
        at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
        at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
        at com.example.fyptrial1.MainActivity.onCreate(MainActivity.java:18)
        at android.app.Activity.performCreate(Activity.java:7136)
        at android.app.Activity.performCreate(Activity.java:7127)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
2019-11-09 13:53:59.983 5972-5972/com.example.fyptrial1 I/Process: Sending signal. PID: 5972 SIG: 9

0 个答案:

没有答案