简体   繁体   中英

Simple dialog error with activity - android.view.WindowManager$BadTokenException

I have the main activity calling a second activity. This second activity calls a viewholder java class from which an alert dialog is called:

                    Log.w(LOG_TAG, "call the alertdialog");
                    showAlertDialog();


public void showAlertDialog() {
    /** define onClickListener for dialog */
    DialogInterface.OnClickListener listener
            = new   DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            // do some stuff eg: context.onCreate(super)
        }
    };

    /** create builder for dialog */
    AlertDialog.Builder builder = new AlertDialog.Builder(NFLActivity.context)
            .setCancelable(false)
            .setMessage("You have made all bets. Select OK to submit...")
            .setTitle("All Bets Are In")
            .setNegativeButton("Return", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {

The app crashes with this: 06-29 10:03:31.340 3740-3740/com.bab_c.picksandbets E/AndroidRuntime: FATAL EXCEPTION: main Process: com.bab_c.picksandbets, PID: 3740 android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?

I have researched to find that the dialog needs an activity but when use the second activity showAlertDialog(SecondActivity.context) it still fails.

First of all, use viewHolder class to just hold the views and call the alert from second Activity using the viewHolder's view.

and second thing, don't make context as static

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