在ValueEventListener onDataChange中调用alertdialog时出现BadTokenException

时间:2018-04-17 05:00:21

标签: android alertdialog

android.view.WindowManager $ BadTokenException:无法添加窗口 - 令牌android.os.BinderProxy@e95f236无效;你的活动在运行吗?

在Firebee事件监听器中使用alertdialog,如下所示。

通常在侦听器中发生错误。

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 个答案:

答案 0 :(得分:0)

您的活动未被附加,这就是为什么它会抛出错误的令牌异常。 检查活动不为空

相关问题