如何修复Java.lang.NullPointerException:错误

时间:2019-04-05 23:35:15

标签: android nullpointerexception

当我单击菜单选项之一以显示片段或新活动时,我希望我的浮动操作按钮快速拨号。但我收到空对象引用错误。 调试菜单显示:

  

java.lang.NullPointerException:尝试调用接口方法   'android.view.MenuItem   android.view.MenuItem.setOnMenuItemClickListener(android.view.MenuItem $ OnMenuItemClickListener)'   在空对象引用上           com.nanodegree.project.oto.DevicesPage $ 1 $ override.duoClicked(DevicesPage.java:97)。

@Override
            public boolean onMenuItemSelected(MenuItem menuItem) {
                switch (menuItem.getItemId()) {
                    case R.id.action_duo:
                        duoClicked();
                        //Toast.makeText(DevicesPage.this, "2 switches", Toast.LENGTH_SHORT).show();
                        fabSpeedDial.closeMenu();
                        return true;
                    case R.id.action_tetra:
                        Toast.makeText(DevicesPage.this, "4 switches", Toast.LENGTH_SHORT).show();
                        fabSpeedDial.closeMenu();
                        return true;
                    case R.id.action_hexa:
                        Toast.makeText(DevicesPage.this, "6 switches", Toast.LENGTH_SHORT).show();
                        fabSpeedDial.closeMenu();
                        return true;

                }
                return true;
            }

            public void duoClicked(){
                MenuItem menuItem = findViewById(R.id.action_duo);
                menuItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        fabDeviceAddingFragment = new FABDeviceAddingFragment();
                        getSupportFragmentManager().beginTransaction()
                                .replace(R.id.container_fragment_deviceAdd,fabDeviceAddingFragment)
                                .commit();
                        return true;
                    }
                });

            }

我希望duoclicked方法可以在同一活动中打开一个片段。

1 个答案:

答案 0 :(得分:0)

您不能访问普通视图之类的菜单项。您必须从菜单访问它们。

执行以下操作:

"[Gmail]\All Mail"

但这没有意义。如果要关闭fab菜单并打开片段,则无需实现另一个菜单项,请单击“侦听器”。

您只需要打开它:

@Override
            public boolean onMenuItemSelected(MenuItem menuItem) {
                switch (menuItem.getItemId()) {
                    case R.id.action_duo:
                        duoClicked(menuItem); // Pass current item
                        //Toast.makeText(DevicesPage.this, "2 switches", Toast.LENGTH_SHORT).show();
                        fabSpeedDial.closeMenu();
                        return true;
                    case R.id.action_tetra:
                        Toast.makeText(DevicesPage.this, "4 switches", Toast.LENGTH_SHORT).show();
                        fabSpeedDial.closeMenu();
                        return true;
                    case R.id.action_hexa:
                        Toast.makeText(DevicesPage.this, "6 switches", Toast.LENGTH_SHORT).show();
                        fabSpeedDial.closeMenu();
                        return true;

                }
                return true;
            }

            public void duoClicked(MenuItem menuItem){
                menuItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        fabDeviceAddingFragment = new FABDeviceAddingFragment();
                        getSupportFragmentManager().beginTransaction()
                                .replace(R.id.container_fragment_deviceAdd,fabDeviceAddingFragment)
                                .commit();
                        return true;
                    }
                });

            }