BadTokenException when call alertdialog in ValueEventListener onDataChange

android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@e95f236 is not valid; is your activity running?

Use alertdialog in the Firebee event listener as shown below.

Often an error occurs in the listener.

private ValueEventListener mInviteListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {

            String token = (String) dataSnapshot.getValue();
            if (token != null) {
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setMessage("accep??");
                builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        ownerLayoutShowHide(OwnerStatus.INIT);

                    }
                });
                builder.setNegativeButton("no", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                                                }
                });
                builder.setCancelable(false);
                mInvitationAlertDialog = builder.create();

                if (!mInvitationAlertDialog.isShowing()) {
                    mInvitationAlertDialog.show();
                }


            } else {
                //none
            }

    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
};

1 answer

  • answered 2018-04-17 05:00 Swati Dongre

    Your activity is not attached that's why its throwing bad token exception. Check activity is not null