尝试隐藏Actionbar项时获取NullPointerException

时间:2015-02-26 22:31:45

标签: android button nullpointerexception android-actionbar hide

当我想从操作栏隐藏操作按钮时,我收到NullPointerException。 我有一个简单的Actionbar和一个Navigationdrawer。 我只想在使用登录对话框成功登录时隐藏操作栏右上角的操作登录按钮。

NavigationDrawerFragment:

@Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        FragmentManager fragmentManager = getFragmentManager();

        Log.d("test12","onOptionsItemSelected");
        if (mDrawerToggle.onOptionsItemSelected(item))
        {
            Log.d("test12_1","onOptionsItemSelected");

            return true;
        }


        if (item.getItemId() == R.id.action_login)
        {
            Log.d("test13", "onOptionItemSelected");
            //Toast.makeText(getActivity(), "Example action.", Toast.LENGTH_SHORT).show();

            final Dialog dialog = new Dialog(getActivity());
            dialog.setContentView(R.layout.login);
            dialog.setTitle(R.string.title_login);

            login_button = (Button) dialog.findViewById(R.id.button_login);
            username = (EditText) dialog.findViewById(R.id.et_username);
            password = (EditText) dialog.findViewById(R.id.et_password);

            login_button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v)
                {
                    Log.d("test14", "Login Button geklickt");
                    if (username.getText().toString().equals("root") && password.getText().toString().equals("root")) {
                        Toast.makeText(getActivity(), "Hello " + username.getText(), Toast.LENGTH_LONG).show();
                        login_status = true;
                        updateAfterLogin(); // Updated die Seitenleiste

                        getActivity().supportInvalidateOptionsMenu(); // reload Actionbar to hide Symbol

                        mDrawerLayout.openDrawer(Gravity.START); // Öffnet den Navigationdrawer
                        dialog.dismiss();
                    }
                    else
                    {
                        Toast.makeText(getActivity(), "Wrong Username or Password!", Toast.LENGTH_SHORT).show();
                    }
                }
            });
            dialog.show();

            return true;
        }

        return super.onOptionsItemSelected(item);
    }

错误显示在以下行:

if(login_status == true)
{
    menu.findItem(R.id.action_login).setVisible(false);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
    Log.d("test11", "onCreateOptionsMenu");
    // If the drawer is open, show the global app actions in the action bar. See also
    // showGlobalContextActionBar, which controls the top-left area of the action bar.
    if (mDrawerLayout != null && isDrawerOpen()) {
        inflater.inflate(R.menu.global, menu);
        showGlobalContextActionBar();
    }

    if(login_status == true)
    {
        menu.findItem(R.id.action_login).setVisible(false);
    }


    super.onCreateOptionsMenu(menu, inflater);
}

错误:

02-26 23:07:14.548  13034-13034/com.example.vo1umen.kimasys E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.vo1umen.kimasys, PID: 13034
    java.lang.NullPointerException: Attempt to invoke interface method 'android.view.MenuItem android.view.MenuItem.setVisible(boolean)' on a null object reference
            at com.example.vo1umen.kimasys.NavigationDrawerFragment.onCreateOptionsMenu(NavigationDrawerFragment.java:408)
            at android.support.v4.app.Fragment.performCreateOptionsMenu(Fragment.java:1868)
            at android.support.v4.app.FragmentManagerImpl.dispatchCreateOptionsMenu(FragmentManager.java:1989)
            at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:276)
            at android.support.v7.app.ActionBarActivity.superOnCreatePanelMenu(ActionBarActivity.java:276)
            at android.support.v7.app.ActionBarActivityDelegate$1.onCreatePanelMenu(ActionBarActivityDelegate.java:79)
            at android.support.v7.app.ActionBarActivityDelegateBase.preparePanel(ActionBarActivityDelegateBase.java:979)
            at android.support.v7.app.ActionBarActivityDelegateBase.doInvalidatePanelMenu(ActionBarActivityDelegateBase.java:1182)
            at android.support.v7.app.ActionBarActivityDelegateBase.access$100(ActionBarActivityDelegateBase.java:79)
            at android.support.v7.app.ActionBarActivityDelegateBase$1.run(ActionBarActivityDelegateBase.java:115)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:792)
            at android.view.Choreographer.doCallbacks(Choreographer.java:596)
            at android.view.Choreographer.doFrame(Choreographer.java:556)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:778)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:155)
            at android.app.ActivityThread.main(ActivityThread.java:5696)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)

1 个答案:

答案 0 :(得分:5)

找不到action_login MenuItem。检查您的R.menu.global xml文件,该项目不在其上。