Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
428 views
in Technique[技术] by (71.8m points)

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...