0

I am giving my design layout below for your kind checking. When I am trying to launch the app, it creates error and showing the message

java.lang.ClassCastException: androidx.drawerlayout.widget.DrawerLayout cannot be cast to com.google.android.material.navigation.NavigationView

main.activity.xml

<?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/drawableLayoutId"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".ModuleActivity"
    tools:openDrawer="start">

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

        <include layout="@layout/toolbar" />

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerViewId"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="8dp"
            android:background="#F0F8F7" />

    </LinearLayout>

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

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:layout_marginBottom="20dp"
            android:gravity="center"
            android:padding="10dp"
            android:text="https://www.stieschool.com"
            android:textColor="#05156E" />

    </com.google.android.material.navigation.NavigationView>

</androidx.drawerlayout.widget.DrawerLayout>

Activity.java

package com.stieschool.addressguide;

import android.os.Bundle;

import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.widget.Toolbar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.navigation.NavigationView;

import java.util.ArrayList;
import java.util.List;

public class ModuleActivity extends AppCompatActivity {

    Toolbar toolbar;
    List<ModuleProperty> modules;
    ModuleRecycleViewAdapter adapter;
    RecyclerView recyclerView;

    DrawerLayout drawerLayout;
    NavigationView navigationView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_module);

        toolbar = findViewById(R.id.toolBarId);
        setSupportActionBar(toolbar);

        drawerLayout =(DrawerLayout) findViewById(R.id.drawableLayoutId);
        navigationView =(NavigationView) findViewById(R.id.drawableLayoutId);
        
        AppUpdateChecker appUpdateChecker=new AppUpdateChecker(this);  
        appUpdateChecker.checkForUpdate(true);

        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.drawer_navigation_open,R.string.drawer_navigation_close);
        drawerLayout.addDrawerListener(toggle);
        toggle.syncState();

        modules=new ArrayList<>();
        modules.add(new ModuleProperty("মন্ত্রণালয়",R.drawable.module_ministry));
        modules.add(new ModuleProperty("হাসপাতাল",R.drawable.module_hospital));
        modules.add(new ModuleProperty("বিশ্ববিদ্যালয় ",R.drawable.module_university));
        modules.add(new ModuleProperty("শপিং সেন্টার",R.drawable.module_shopping_center));
        modules.add(new ModuleProperty("পার্ক",R.drawable.module_park));
        modules.add(new ModuleProperty("চিড়িয়াখানা ",R.drawable.module_zoo));
        modules.add(new ModuleProperty("ঐতিহাসিক স্থান",R.drawable.module_historial_place));
        modules.add(new ModuleProperty("সমুদ্র সৈকত",R.drawable.module_sea_beach));
        modules.add(new ModuleProperty("বন-পাহাড়",R.drawable.module_forest));
        modules.add(new ModuleProperty("পাহাড়ি ঝর্ণা",R.drawable.module_fountain));
        modules.add(new ModuleProperty("কমিউনিটি সেন্টার",R.drawable.module_community_center));
        modules.add(new ModuleProperty("পোস্ট অফিস",R.drawable.module_postoffice));
        modules.add(new ModuleProperty("কুরিয়ার সার্ভিস",R.drawable.module_curiar_service));
        modules.add(new ModuleProperty("আবাসিক হোটেল",R.drawable.module_abasik_hotel));
        modules.add(new ModuleProperty("রেস্টুরেন্ট",R.drawable.module_resturant));

        recyclerView=findViewById(R.id.recyclerViewId);
        adapter=new ModuleRecycleViewAdapter(this,modules);
        recyclerView.setLayoutManager(new GridLayoutManager(this,3));
        recyclerView.setAdapter(adapter);
    }
}

I am have shared Java Code for your kind checking.

Regards Tojib

Ichigo Kurosaki
  • 3,765
  • 8
  • 41
  • 56
  • This is `RuntimeException` in your java activity/fragment .. so please share your behavior – Zain Jan 01 '21 at 05:49
  • I think you are doing mistake in findViewById(). make sure you are assigning it to correct object of `DrawerLayout` – Priyanka Jan 01 '21 at 05:54
  • Please share java code too – Chintan Jan 01 '21 at 06:16
  • Dear Priyankagb, Thank you for replying. I have edited the question and added Java code for your kind checking. – Tojibul Islam Jan 01 '21 at 08:39
  • Dear Chintan, I have added Java code for your kind checking – Tojibul Islam Jan 01 '21 at 12:32
  • Sorry, It was my mistake in java code, ```drawerLayout =(DrawerLayout) findViewById(R.id.drawableLayoutId); navigationView =(NavigationView) findViewById(R.id.drawableLayoutId);``` I did copy paste and did not changed the ***navigationNewId*** !! Now after changing it is working. – Tojibul Islam Jan 01 '21 at 17:05

0 Answers0