SoFunction
Updated on 2025-03-11

Android development solves the problem of popupWindow overlap error reporting

This error will be reported when popupWindow pops up again in popupWindow

ERROR/AndroidRuntime(888): $BadTokenException: Unable to add window -- token $W@44ef1b68 is not valid; is your activity running? 

The meaning of an error is roughly that the dependent activity is gone.

Solution 1

Don't continue to show a popupWindow in the current popupWindow. You should write an interface callback to the Activity to show.

Solution 2

If it is just a simple pop-up window reminder, change the popup windows that pop up the second time to Toast,

public class VerifySuccessDialog extends Toast {
  public VerifySuccessDialog(Context context) {
    super(context);
    //Set toast View    setView((context).inflate(.include_popwindow_verify, null));
    //Popt position    setGravity(, 0, 0);
    //Duration    setDuration(Toast.LENGTH_SHORT);
  }
}

Where to use

new VerifySuccessDialog(mActivity).show();

Solution 3

The second pop-up window is changed to Dialog, set the pattern of the dialog, and add it

<style name="VerifyDialog" parent="@android:style/">
    <item name="android:windowBackground">@drawable/trans</item>
    <item name="android:windowFrame">@null</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:backgroundDimEnabled">true</item>
    <item name="android:windowSoftInputMode">adjustPan</item>
  </style>

Define a Dialog class

public class VerifySuccessDialog extends Dialog {
  // Used to set the time to automatically close  private int showTime;
  public VerifySuccessDialog(Context context) {
    super(context, );
    setContentView(.include_popwindow_verify);
  }
  public int getShowTime() {
    return showTime;
  }
  public void setShowTime(int showTime) {
     = showTime;
  }
}

Just use it directly:

VerifySuccessDialog dialog = new VerifySuccessDialog(mActivity);

Where to display

();

Summarize

The above is the entire content of this article. I hope that the content of this article has certain reference value for your study or work. Thank you for your support. If you want to know more about it, please see the relevant links below