加载AlertDialog.Builer时出错 - ANDROID

时间:2015-12-11 01:51:27

标签: android alert alertdialog

我需要一些帮助!!

当我使用TurnInternetOn()方法时,“helper”变量总是 false ;

我尝试调试它,当调试器传递正面和负面按钮的创建时,它不会传递给我的BoxHelper()方法。

有人知道为什么吗?

private void TurnInternetOn(final Context context) {
    HelpersLoading helpersLoading = new HelpersLoading();

    String message = context.getResources().getString(R.string.use_wifi);
    String positiveButton = context.getResources().getString(R.string.turn_wifi_on);
    String negativeButton = context.getResources().getString(R.string.keep_3g);

    boolean helper = helpersLoading.LoadBox(context, message, positiveButton, negativeButton);
    if (helper) { // helper always comes false
        context.startActivity(new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK));
    } else {
        dialog.dismiss();
    }
}

CLASS:HelpersLoading

// Handle the PopUp in the middle of screen
public boolean LoadBox(Context context, String message, String positiveButton, String negativeButton) {
    AlertDialog.Builder builderaction = new AlertDialog.Builder(context);
    builderaction.setTitle(context.getResources().getString(R.string.atention));
    builderaction.setMessage(message);
    builderaction.setPositiveButton(positiveButton, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            BoxHelper(true);
        }
    });

    builderaction.setNegativeButton(negativeButton, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            BoxHelper(false);
        }
    });

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

    return this.returning;
}

CLASS:HelpersLoading - Aux

// Aux of LoadBox
public void BoxHelper(boolean temp) {

    this.returning = temp;
}

1 个答案:

答案 0 :(得分:0)

在用户有机会看到AlertDialog之前,您的LoadBox方法正在返回。对话框设计为不同步。您可能需要考虑放置以下行:

context.startActivity(new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK));

在AlertDialog正面onClick方法内部。