Android Development Solves popupWindow Overlapping Error Reporting Problem

  • 2021-10-16 02:35:33
  • OfStack

This error will be reported when popupWindow pops up in popupWindow


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

Reporting an error probably means that the dependent Activity is gone.

Solution 1

Do not continue show1 popupWindow in the current popupWindow, but write an interface callback to Activity for show.

Solution 2

If it's just a simple pop-up reminder, change the popupwindows popped up for the second time to Toast.


public class VerifySuccessDialog extends Toast {
  public VerifySuccessDialog(Context context) {
    super(context);
    // Settings toast Adj. View
    setView(LayoutInflater.from(context).inflate(R.layout.include_popwindow_verify, null));
    // Ejection position 
    setGravity(Gravity.CENTER, 0, 0);
    // Duration 
    setDuration(Toast.LENGTH_SHORT);
  }
}

The place to use it is


new VerifySuccessDialog(mActivity).show();

Solution 3

Change the second pop-up window to Dialog, set style of dialog, and add it in Style. xml


<style name="VerifyDialog" parent="@android:style/Theme.Holo.Dialog.NoActionBar">
    <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 1 Dialog class


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

Use directly:


VerifySuccessDialog dialog = new VerifySuccessDialog(mActivity);

Where to display

dialog.show();

Summarize


Related articles: