如何在android中启动另一个警告对话框?

时间:2016-04-10 06:15:05

标签: android alertdialog android-alertdialog

我要做的是,我已从主要活动中启动了一个警告对话框。用户必须解决基本数学并单击肯定按钮。如果他成功了,我希望再次显示相同的警告对话框。基本上我希望用户成功解决数学3次(显示相同的警报对话框3次)。下面的代码在注释行引发异常:

IllegalStateException:
The specified child already has a parent. You must call removeView() on the child's parent first.

我该如何解决这个问题?

public class SolveMath extends DialogFragment {

MyDialog myDialog;
int count = 0;


@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    myDialog = (MyDialog) activity;
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    LayoutInflater inflater = getActivity().getLayoutInflater();
    final View view = inflater.inflate(R.layout.dialog_layout, null);
    final ComponentName component = new ComponentName(view.getContext(), BlockOutgoingCall.class);
    final Globals globals = ((Globals) view.getContext().getApplicationContext());


    builder.setView(view);
    builder.setCancelable(false);
    builder.setTitle("Solve!");

    Random r = new Random();
    int min = 50;
    int max = 500;
    final int i1 = r.nextInt(max - min + 1) + min;
    final int i2 = r.nextInt(max - min + 1) + min;

    TextView math = (TextView) view.findViewById(R.id.math);
    String solve = i1 + "+" + i2;
    math.setText(solve);

    builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            Toast.makeText(getActivity(), "You're still drunk!", Toast.LENGTH_LONG).show();
            globals.setGlobalVarValue("true");
        }
    });

    builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {


            EditText mathans = (EditText) view.findViewById(R.id.mathans);

            if (mathans.getText().toString().trim().equals("")) {
                mathans.setError("Field Empty!");
                Toast.makeText(getActivity(), "Please Enter Value", Toast.LENGTH_LONG).show();
            } else {
                int abc = Integer.parseInt(mathans.getText().toString());
                if (abc == (i1 + i2)) {
                    //   globals.setGlobalIntValue(count);
                     if (count == 3) {

                    Toast.makeText(getActivity(), "You are good to go!", Toast.LENGTH_LONG).show();
                    globals.setGlobalVarValue("false");
                    view.getContext().getPackageManager().setComponentEnabledSetting(component, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
                    myDialog.showResult(true);
                        count = 0;
                   } else {

                      count++;
                     builder.show();  //throwing exception here
                    }
                } else {
                    Toast.makeText(getActivity(), "Sorry, wrong answer, try again!", Toast.LENGTH_LONG).show();
                    globals.setGlobalVarValue("true");
                    myDialog.showResult(false);
                }
            }
        }
    });

    Dialog dialog = builder.create();
    return dialog;
}


public interface MyDialog {
    public void showDialog();

    public void showResult(boolean b);
}

}

1 个答案:

答案 0 :(得分:0)

这里可能是一个解决方案,不必再次启动对话框。首先,将i1i2作为SolveMath的类变量。然后,您可以创建一个函数,例如generateMathProblem

private void generateMathProblem() {
    Random r = new Random();
    int min = 50;
    int max = 500;
    i1 = r.nextInt(max - min + 1) + min;
    i2 = r.nextInt(max - min + 1) + min;

    TextView math = (TextView) view.findViewById(R.id.math);
    String solve = i1 + "+" + i2;
    math.setText(solve);
}

最后将builder.show();替换为generateMathProblem();