PopupWindow $BadTokenException: Unable to add window — token null is not valid

后端 未结 11 2101

I have the following error when showing a PopupWindow. The errors are triggered by the line:

checkInPopup.showAtLocation((ViewGroup) mapView.getParent(), Gra         


        
相关标签:
11条回答
  • 2020-11-27 05:29

    you are showing your popup too early. You may post a delayed runnable for showatlocation in Onresume , Give it a try

    Edit: This post seems to have the same problem answered Problems creating a Popup Window in Android Activity

    0 讨论(0)
  • 2020-11-27 05:30

    use this method before show

    public static ViewGroup getActivityFirstLayout(Context ctx)
    {
        return (ViewGroup)((ViewGroup) ActivityMaster.getActivity(ctx).findViewById(android.R.id.content)).getChildAt(0);
    }
    
    private boolean activityIsOk(Activity activity)
    {
        boolean s1 = !(activity == null || activity.isFinishing());
    
        if(s1)
        {
            View v = LayoutMaster.getActivityFirstLayout(activity);
            return v.isShown() && ViewCompat.isLaidOut(v);
        }
    
        return false;
    }
    
    0 讨论(0)
  • 2020-11-27 05:31

    try to show popup like this

    new Handler().postDelayed(new Runnable(){
    
        public void run() {
           popupWindow.showAtLocation(context.getWindow().getDecorView(), Gravity.CENTER,0,0);
        }
    
    }, 200L);
    
    0 讨论(0)
  • 2020-11-27 05:34

    A popup's parent can't itself be a popup. Both of their parents must be the same. So, if you create a popup inside a popup, you must save the parent's popup and make it a parent.

    here's an example

    0 讨论(0)
  • 2020-11-27 05:40

    Same problem happened with me when i try to show popup menu in activity i also got same excpetion but i encounter problem n resolve by providing context

    YourActivityName.this instead of getApplicationContext() at

    Dialog dialog = new Dialog(getApplicationContext());

    and yes it worked for me may it will help someone else

    0 讨论(0)
提交回复
热议问题