Android - 无法通过应用内计费关闭 AlertDialog

发布于 2024-11-30 02:32:23 字数 747 浏览 0 评论 0原文

我在使用用于启动应用内购买的 AlertDialog 时遇到问题。当用户点击“购买”按钮时,应用内购买将按预期触发,但 AlertDialog 不会关闭。然后,当应用程序内购买完成时,程序返回到我的应用程序,但 AlertDialog 仍然打开。

如果我注释掉 buyCard() 函数,AlertDialog 将关闭。知道为什么当涉及应用内计费时 AlertDialog 没有关闭吗?

final CharSequence[] items = {"Buy","Close"};

AlertDialog.Builder builder = new AlertDialog.Builder(Card.this);
builder.setTitle("Want to Buy?");
builder.setItems(items, new DialogInterface.OnClickListener() {

    public void onClick(DialogInterface dialog, int item) {
        if(item == 0) { // BUY THE ITEM
            dialog.dismiss();
            buyCard();
        } else if (item == 1) { // Don't Buy
            dialog.dismiss();
        }
    }
});

AlertDialog alert = builder.create();
alert.show();

I am having a problem with an AlertDialog that I am using to initiate an In-App Purchase. When the user taps the "Buy" button, the In-App Purchase fires as expected, but the AlertDialog does not close. Then, when the In-App Purchase finishes, the program returns to my app but the AlertDialog is still open.

If I comment out the buyCard() function, the AlertDialog will close. Any idea why the AlertDialog is not closing when the In-App Billing is involved?

final CharSequence[] items = {"Buy","Close"};

AlertDialog.Builder builder = new AlertDialog.Builder(Card.this);
builder.setTitle("Want to Buy?");
builder.setItems(items, new DialogInterface.OnClickListener() {

    public void onClick(DialogInterface dialog, int item) {
        if(item == 0) { // BUY THE ITEM
            dialog.dismiss();
            buyCard();
        } else if (item == 1) { // Don't Buy
            dialog.dismiss();
        }
    }
});

AlertDialog alert = builder.create();
alert.show();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黒涩兲箜 2024-12-07 02:32:23

我确信这个问题早已得到解决,但可能的解决方案是将buyCard();移至dialog.dismiss();上方

所以,就像这样:

    if(item == 0) { // BUY THE ITEM
        buyCard();
        dialog.dismiss();
    } else if (item == 1) { // Don't Buy
        dialog.dismiss();
    }

原因是,对话框无法运行 buyCard(); 行,因为您已经将其关闭。有点像尝试在 return 语句之后运行代码。

I'm sure this question has long been figured out, but the probable solution would be to move buyCard(); to above dialog.dismiss();

So, like this:

    if(item == 0) { // BUY THE ITEM
        buyCard();
        dialog.dismiss();
    } else if (item == 1) { // Don't Buy
        dialog.dismiss();
    }

Reason being, the dialog can't run the buyCard(); line since you've already dismissed it. Kind of like trying to run code after a return statement.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文