简体   繁体   中英

Actionbar menu item selection - token null is not for an application

I'm trying to use the menu item objects to set onclick listeners for each item. I keep getting a null pointer on the item selected. What am I doing wrong?

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_activity_actions, menu);
    return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int itemId_ = item.getItemId();
    if (itemId_ == R.id.main_activity_actions_cancel) {
        new AlertDialog.Builder(getApplicationContext())
                .setIcon(android.R.drawable.ic_dialog_alert)
                .setTitle("Exit Search Results?")
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //Stop the activity
                        finish();
                    }
                })
                .setNegativeButton("No!", null)
                .show();
        return true;
    }
    else if (itemId_ == R.id.main_activity_actions_action_search) {
        final AlertDialog.Builder alert = new AlertDialog.Builder(getApplicationContext());
        final EditText input = new EditText(getApplicationContext());
        input.setHint("type here...");
        alert.setView(input);
        alert.setPositiveButton("Search", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                String value = input.getText().toString().trim();
                start_searching_activity(value.trim());
            }
        });

        alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                dialog.cancel();
            }
        });
        alert.show();
        return true;
    }
            return false;
}

Log:

 05-10 13:13:11.578  25113-25113/com.secret E/uncaught exception!!! android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
            at android.view.ViewRootImpl.setView(ViewRootImpl.java:691)
            at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:288)
            at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
            at android.app.Dialog.show(Dialog.java:312)
            at android.app.AlertDialog$Builder.show(AlertDialog.java:991)
            at ads_swiped.secretsCardDealer.onOptionsItemSelected(secretsCardDealer.java:339)
            at android.app.Activity.onMenuItemSelected(Activity.java:3024)
            at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:373)
            at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:1199)
            at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:761)
            at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:155)
            at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:904)
            at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:894)
            at android.widget.ActionMenuView.invokeItem(ActionMenuView.java:611)
            at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:180)
            at android.view.View.performClick(View.java:5197)
            at android.view.View$PerformClick.run(View.java:20909)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:5944)
            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:1399)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

Replace getApplicationContext() with getActivity() in

final AlertDialog.Builder alert = new AlertDialog.Builder(getApplicationContext());

The ApplicationContext should not be used for tasks such as creating Dialogs. As you are in a fragment you can instead get the Activity-Context simply by calling the Fragments getActivity() method.

UPDATE

Since this is a FragmentActivity, you just need to call this to get the Activity.See this for more details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM