Почему Android не поддерживает панель навигации? - PullRequest
0 голосов
/ 09 ноября 2019

Итак, я пытаюсь завершить навигацию с помощью этого учебника (https://www.youtube.com/watch?v=fGcMLu1GJEc&t=16s), однако, я понимаю, что что-то изменилось в Android Studio, так как учебник был загружен с AndroidX, панелями инструментов и прочим, но я 'Я не уверен, что изменилось.

Я часами пытался разобраться в проблеме, но я новичок в 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

Ответы [ 3 ]

0 голосов
/ 09 ноября 2019

Ошибка говорит о том, что вы используете макет выдвижного ящика из вспомогательного компата, вы должны использовать макет выдвижного ящика для материала

Вот как добавить библиотеку

https://material.io/develop/android/docs/getting-started/

Вот как добавить ящик

https://material.io/develop/android/components/navigation-view/

0 голосов
/ 10 ноября 2019

AndroidX заменяет исходные API библиотек поддержки пакетами в пространстве имен androidx.

Похоже, что вы используете некоторые из классов библиотек поддержки в xmls, как вы можете видеть в журналах:

Didn't find class "android.support.v4.widget.DrawerLayout" on path: DexPathList
    android.support.v4.widget.DrawerLayout

В вашем xml, который раздувается в MainActivity, замените это:

android.support.v4.widget.DrawerLayout

следующим:

androidx.drawerlayout.widget.DrawerLayout

Это должно решить эту ошибку для вас.

Тем не менее, я уверен, что будет больше xmls со старыми классами поддержки библиотеки. См. здесь и найдите класс, вызывающий ошибку, как указано выше, и замените его соответствующим классом androidx.

0 голосов
/ 09 ноября 2019

удалить 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>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...