3

I am trying to insert RecyclerView inside DrawerLayout but the app crashes when open the activity that has the design.

The DrawerLayout is very important for the NavigationView to work. RecyclerView should be under the ConstraintLayout that holds the ToolBar.

<?xml version="1.0" encoding="utf-8"?>
<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/drawerlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/artboard">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <!--toolbar-->
        <Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            android:orientation="horizontal"
            android:paddingStart="15dp"
            android:paddingEnd="15dp"
            app:layout_constraintTop_toTopOf="parent">

            <ImageView
                android:id="@+id/menuIcon"
                android:layout_width="36dp"
                android:layout_height="36dp"
                android:src="@drawable/ic_menu"
                app:itemIconTint="@color/colorWhite" />

            <ImageView
                android:id="@+id/cartIcon"
                android:layout_width="36dp"
                android:layout_height="36dp"
                android:layout_gravity="end"
                android:src="@drawable/ic_cart" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="22dp"
                android:fontFamily="@font/caveat_bold"
                android:text="storePerfect"
                android:textColor="@color/colorWhite"
                android:textSize="26sp" />

        </Toolbar>
        <!--end of tool bar-->

    </androidx.constraintlayout.widget.ConstraintLayout>

            <!--        design here-->
            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recycler_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="55dp">

            </androidx.recyclerview.widget.RecyclerView>



    <com.google.android.material.navigation.NavigationView
        android:id="@+id/navigationView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/layout_navigation_header"
        app:menu="@menu/navigation_menu" />

</androidx.drawerlayout.widget.DrawerLayout>



How can I fix this problem without affecting other layouts?

Error I get...


01/11 00:31:46: Launching 'app' on Nexus 6 API 28.
Install successfully finished in 688 ms.
$ adb shell am start -n "com.example.payment_screen/com.example.payment_screen.Store_Activity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Connected to process 15174 on device 'Nexus_6_API_28 [emulator-5554]'.
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
W/ComponentDiscovery: Class com.google.firebase.dynamicloading.DynamicLoadingRegistrar is not an found.
I/FirebaseApp: Device unlocked: initializing all Firebase APIs for app [DEFAULT]
D/FirebaseAuth: Notifying id token listeners about user ( APcX0F7Se1ffN57bTkC73QdNcwz2 ).
I/FirebaseInitProvider: FirebaseApp initialization successful
I/FirebaseAuth: [FirebaseAuth:] Preparing to create service connection to fallback implementation
W/.payment_scree: Accessing hidden method Landroid/graphics/drawable/Drawable;->getOpticalInsets()Landroid/graphics/Insets; (light greylist, linking)
    Accessing hidden field Landroid/graphics/Insets;->left:I (light greylist, linking)
    Accessing hidden field Landroid/graphics/Insets;->right:I (light greylist, linking)
    Accessing hidden field Landroid/graphics/Insets;->top:I (light greylist, linking)
    Accessing hidden field Landroid/graphics/Insets;->bottom:I (light greylist, linking)
W/.payment_scree: Accessing hidden method Landroid/view/View;->getAccessibilityDelegate()Landroid/view/View$AccessibilityDelegate; (light greylist, linking)
W/.payment_scree: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (light greylist, reflection)
W/.payment_scree: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (light greylist, reflection)
W/.payment_scree: Accessing hidden method Landroid/graphics/FontFamily;-><init>()V (light greylist, reflection)
W/.payment_scree: Accessing hidden method Landroid/graphics/FontFamily;->addFontFromAssetManager(Landroid/content/res/AssetManager;Ljava/lang/String;IZIII[Landroid/graphics/fonts/FontVariationAxis;)Z (light greylist, reflection)
    Accessing hidden method Landroid/graphics/FontFamily;->addFontFromBuffer(Ljava/nio/ByteBuffer;I[Landroid/graphics/fonts/FontVariationAxis;II)Z (light greylist, reflection)
    Accessing hidden method Landroid/graphics/FontFamily;->freeze()Z (light greylist, reflection)
    Accessing hidden method Landroid/graphics/FontFamily;->abortCreation()V (light greylist, reflection)
    Accessing hidden method Landroid/graphics/Typeface;->createFromFamiliesWithDefault([Landroid/graphics/FontFamily;Ljava/lang/String;II)Landroid/graphics/Typeface; (light greylist, reflection)
I/.payment_scree: NativeAlloc concurrent copying GC freed 265(47KB) AllocSpace objects, 0(0B) LOS objects, 50% free, 1581KB/3MB, paused 484us total 113.036ms
D/OpenGLRenderer: Skia GL Pipeline
D/: HostConnection::get() New Host Connection established 0xc9cf13c0, tid 15206
I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
    android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasHDRDisplay retrieved: 0
I/OpenGLRenderer: Initialized EGL, version 1.4
D/OpenGLRenderer: Swap behavior 1
W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
D/OpenGLRenderer: Swap behavior 0
D/EGL_emulation: eglCreateContext: 0xdd37b7a0: maj 3 min 1 rcv 4
D/EGL_emulation: eglMakeCurrent: 0xdd37b7a0: ver 3 1 (tinfo 0xdd3901a0)
E/eglCodecCommon: glUtilsParamSize: unknow param 0x000082da
    glUtilsParamSize: unknow param 0x000082da
W/.payment_scree: Accessing hidden field Landroid/os/Trace;->TRACE_TAG_APP:J (light greylist, reflection)
    Accessing hidden method Landroid/os/Trace;->isTagEnabled(J)Z (light greylist, reflection)
    Accessing hidden method Landroid/os/Trace;->asyncTraceBegin(JLjava/lang/String;I)V (light greylist, reflection)
    Accessing hidden method Landroid/os/Trace;->asyncTraceEnd(JLjava/lang/String;I)V (light greylist, reflection)
    Accessing hidden method Landroid/os/Trace;->traceCounter(JLjava/lang/String;I)V (light greylist, reflection)
D/EGL_emulation: eglMakeCurrent: 0xdd37b7a0: ver 3 1 (tinfo 0xdd3901a0)
W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@9810d75
W/.payment_scree: Accessing hidden field Landroid/view/WindowInsets;->CONSUMED:Landroid/view/WindowInsets; (light greylist, reflection)
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.payment_screen, PID: 15174
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.payment_screen/com.example.payment_screen.My_Product_List}: android.view.InflateException: Binary XML file line #62: Binary XML file line #62: Error inflating class <unknown>
        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 #62: Binary XML file line #62: Error inflating class <unknown>
     Caused by: android.view.InflateException: Binary XML file line #62: Error inflating class <unknown>
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.newInstance0(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
        at android.view.LayoutInflater.createView(LayoutInflater.java:647)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
        at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:696)
        at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:170)
        at com.example.payment_screen.My_Product_List.onCreate(My_Product_List.java:29)
        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)
     Caused by: android.view.InflateException: Couldn't resolve menu item onClick handler onNavigationItemSelected in class com.example.payment_screen.My_Product_List
        at androidx.appcompat.view.SupportMenuInflater$InflatedOnMenuItemClickListener.<init>(SupportMenuInflater.java:255)
        at androidx.appcompat.view.SupportMenuInflater$MenuState.setItem(SupportMenuInflater.java:484)
        at androidx.appcompat.view.SupportMenuInflater$MenuState.addItem(SupportMenuInflater.java:531)
E/AndroidRuntime:     at androidx.appcompat.view.SupportMenuInflater.parseMenu(SupportMenuInflater.java:207)
        at androidx.appcompat.view.SupportMenuInflater.inflate(SupportMenuInflater.java:129)
        at com.google.android.material.navigation.NavigationView.inflateMenu(NavigationView.java:362)
        at com.google.android.material.navigation.NavigationView.<init>(NavigationView.java:230)
        at com.google.android.material.navigation.NavigationView.<init>(NavigationView.java:119)
            ... 28 more
     Caused by: java.lang.NoSuchMethodException: onNavigationItemSelected [interface android.view.MenuItem]
        at java.lang.Class.getMethod(Class.java:2068)
        at java.lang.Class.getMethod(Class.java:1690)
        at androidx.appcompat.view.SupportMenuInflater$InflatedOnMenuItemClickListener.<init>(SupportMenuInflater.java:251)
            ... 35 more
I/Process: Sending signal. PID: 15174 SIG: 9

NavigationHeader file

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:padding="20dp">




    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:ignore="MissingConstraints"
        android:orientation="horizontal">

        <com.makeramen.roundedimageview.RoundedImageView
            android:id="@+id/image_profile"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:scaleType="centerCrop"
            android:src="@drawable/ic_image_profile_default"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:riv_oval="true"/>


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_gravity="center">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="Username"
                android:textSize="16sp"
                android:paddingStart="12dp"
                android:textStyle="bold"
                android:gravity="center_vertical"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="someone@email.com"
                android:textSize="14sp"
                android:paddingStart="12dp"
                android:gravity="center_vertical"/>

        </LinearLayout>



    </LinearLayout>



</androidx.constraintlayout.widget.ConstraintLayout>

activity final design

Zyad Reda
  • 41
  • 4

3 Answers3

0

The reason for this is probably because you have more than 2 children inside DrawerLayout. DrawerLayout is allowed only 2 childs views of which

  1. ContentView
  2. NavigationView

So your first view needs to be the content you'll show inside this layout. Puting RecylcerView inside the ConstraintLayout would fix this, you just need to set RecylcerView below the ConstraintLayout. It should look like this:

<?xml version="1.0" encoding="utf-8"?>
<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/drawerlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/artboard">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <!--toolbar-->
        <Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            android:orientation="horizontal"
            android:paddingStart="15dp"
            android:paddingEnd="15dp"
            app:layout_constraintTop_toTopOf="parent">

            <ImageView
                android:id="@+id/menuIcon"
                android:layout_width="36dp"
                android:layout_height="36dp"
                android:src="@drawable/ic_menu"
                app:itemIconTint="@color/colorWhite" />

            <ImageView
                android:id="@+id/cartIcon"
                android:layout_width="36dp"
                android:layout_height="36dp"
                android:layout_gravity="end"
                android:src="@drawable/ic_cart" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="22dp"
                android:fontFamily="@font/caveat_bold"
                android:text="storePerfect"
                android:textColor="@color/colorWhite"
                android:textSize="26sp" />

        </Toolbar>
        <!--end of tool bar-->

        <!--        design here-->
        <androidx.recyclerview.widget.RecyclerView //RECYCLER VIEW INSIDE CONSTRAINTLAYOUT
            android:id="@+id/recycler_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="55dp">

        </androidx.recyclerview.widget.RecyclerView>
    </androidx.constraintlayout.widget.ConstraintLayout>
    
    <com.google.android.material.navigation.NavigationView
        android:id="@+id/navigationView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/layout_navigation_header"
        app:menu="@menu/navigation_menu" />

</androidx.drawerlayout.widget.DrawerLayout>
SlothCoding
  • 1,546
  • 7
  • 14
  • I tried it, but not working. still crashes. – Zyad Reda Jan 10 '21 at 22:36
  • What is your "My_Product_List", it says: "Binary XML file line #62: Error inflating class " Did you added material dependencies: implementation 'com.google.android.material:material:1.2.1' to your gradle? And is your Theme under MaterialTheme? – SlothCoding Jan 10 '21 at 22:40
0

Since you are manually configuring toolbar. Please do the following:

  1. Make sure your Application Theme does not have Toolbar.

    for example: in styles.xml / theme.xml It should be: <style name="Theme.AAD" parent="Theme.MaterialComponents.Light.NoActionBar">

    Kindly note the last string NoActionBar

  2. Secondly, in your activity onCreate, configure your toolbar using setSupportActionBar(binding.toolbar)

    Note: you can also use findViewByID instead of binding

After this, in Android Studio, click on File > Invalidate Caches/Restart and Run your app again.

This should solve the problem mentioned above.
If not then please post Main Activity, Theme.xml and all other code.

SVK
  • 676
  • 1
  • 5
  • 17
0

Make a new .xml file and add into activity_main.xml

Include