我无法获得菜单项的视图引用

问题描述

我试图获取对 ExpandableListView 的引用,我尝试通过绑定、R.id、公共布尔 onCreateOptionsMenu(Menu menu) 来访问它,但它们都不起作用,因为我每次都收到 NullPointerException。

我的 main.java 文件

 ExpandableListView expandableListView;
 private DrawerActivityBinding binding;

 ArrayList<String> listGroup = new ArrayList<>();
 HashMap<String,ArrayList<String>> listChild = new HashMap<>();
 MainAdapter adapter;
 private DrawerLayout drawer;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     //setContentView(R.layout.drawer_activity);
     binding = DrawerActivityBinding.inflate(getLayoutInflater());
     setContentView(binding.getRoot());

     Toolbar toolbar = findViewById(R.id.toolbar);
     setSupportActionBar(toolbar);
     drawer = findViewById(R.id.drawer_layout);

     binding = DrawerActivityBinding.inflate(getLayoutInflater());
     setContentView(binding.getRoot());



     expandableListView = (ExpandableListView) findViewById(R.id.exp_list);

     for (int g = 0; g <= 10; g++){
         listGroup.add("group" + 1);
         ArrayList<String> arrayList = new ArrayList<>();
         for (int c = 0; c<= 5; c++) {
             arrayList.add("Item" + c);

         }
         listChild.put(listGroup.get(g),arrayList);
     }
     adapter = new MainAdapter(listGroup,listChild);
     expandableListView.setAdapter(adapter);


     ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this,drawer,toolbar,R.string.opendrawer,R.string.closeddrawer);
     drawer.addDrawerListener(toggle);
     toggle.syncState();
 }
 @Override
 public void onBackpressed() {
     if (drawer.isDrawerOpen(GravityCompat.START)) {
         drawer.closeDrawer(GravityCompat.START);
     } else {
         super.onBackpressed();
     }


 }

}

drawer_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/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.platform.DrawerActivity"
    tools:openDrawer="start">

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

        
    </LinearLayout>

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/header"
        app:menu="@menu/activity_main_drawer"/>
    
</androidx.drawerlayout.widget.DrawerLayout>

activity_main_drawer.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/navigation"
    >

    <ExpandableListView
        android:id="@+id/exp_list">


    </ExpandableListView>

</menu>

我应该如何处理这个问题?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)