微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决我无法获得菜单项的视图引用

我试图获取对 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 举报,一经查实,本站将立刻删除。