首页 > 解决方案 > androidx.drawerlayout.widget.DrawerLayout 无法转换为 com.google.android.material.navigation.NavigationView

问题描述

我在下面给出我的设计布局供您检查。当我尝试启动应用程序时,它会产生错误并显示消息

java.lang.ClassCastException:androidx.drawerlayout.widget.DrawerLayout 无法转换为 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>

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

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

我已共享Java 代码供您检查。

问候托吉布

标签: javaandroiddrawerlayoutnavigationview

解决方案


推荐阅读