为什么androidx不支持导航栏? [英] Why won't androidx support the navigation bar?
问题描述
因此,我正在尝试使用本教程完成导航抽屉( https://www.youtube.com/watch?v=fGcMLu1GJEc&t=16s ),但是,我不明白,自从教程使用AndroidX和工具栏及其他内容上传后,Android Studio发生了一些变化,但是我不确定发生了什么变化.
So I'm trying to get a navigation drawer done using this tutorial (https://www.youtube.com/watch?v=fGcMLu1GJEc&t=16s) however, I undersetand that something has changed in Android Studio since the tutorial was uploaded with AndroidX and toolbars and stuff, but I'm not sure what has changed.
我已经尝试了几个小时才能理解问题,但是我是android studio的新手,所以我一无所知.该应用程序一直崩溃
I've been trying for hours to understand the problem but I'm new to android studio so I'm clueless. The app just keeps crashing
基本上,我只是不知道要使工具栏生效的内容是什么
Basically, I just don't know what to import in regards to the toolbar to make it work
这是与之相关的主要活动信息
This is the main activity info thats relevant
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
推荐答案
AndroidX用androidx名称空间中的包替换了原始的支持库API.
AndroidX replaces the original support library APIs with packages in the androidx namespace.
就像您在日志中看到的那样,您似乎在xml中使用了一些支持库类:
Looks like you are using some of the support library classes in your xmls as you can see in the logs:
Didn't find class "android.support.v4.widget.DrawerLayout" on path: DexPathList
android.support.v4.widget.DrawerLayout
在MainActivity中被夸大的xml中,替换为:
In your xml that is being inflated in the MainActivity, replace this:
android.support.v4.widget.DrawerLayout
与此:
androidx.drawerlayout.widget.DrawerLayout
这应该为您解决此错误.
This should solve this error for you.
但是,我很肯定会有更多的XML与较早的支持库类一起使用.请在此处中进行搜索,然后按上述步骤搜索导致错误的类并进行替换并带有相应的androidx类.
However, I am positive there will be more xmls with older support library classes. Refer here and search for the class causing the error as above and replace it with the corresponding androidx class.
这篇关于为什么androidx不支持导航栏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!