0

这是我的代码:

    public class MainActivity extends Activity {

    private static final int CONFIRMATION_DIALOG = 0;
    private View mLoginConfirmView;
    private TextView mTextViewLoginConfirm;
    private CheckBox mCheckBoxLoginConfirm;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mLoginConfirmView = View.inflate(this, R.layout.dialog_login_confirmation, null);
        mTextViewLoginConfirm = (TextView) mLoginConfirmView.findViewById(R.id.textView_DialogTranspConfirm);
        mCheckBoxLoginConfirm = (CheckBox) mLoginConfirmView.findViewById(R.id.checkBox_DialogTranspConfirm);

        showDialog(CONFIRMATION_DIALOG);
    }

    @Override
    @Deprecated
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case CONFIRMATION_DIALOG: {


            return new AlertDialog.Builder(this)
            .setIcon(android.R.drawable.stat_sys_warning)
            .setTitle(R.string.app_name)
            .setView(mLoginConfirmView)
            .setPositiveButton(R.string.change, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                    dialog.dismiss();

                    showDialog(EXIT_PROGRESS_DIALOG);
                }
            })
            .setNegativeButton(R.string.go_ahead, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                    dialog.dismiss();

                    if (mCheckBoxLoginConfirm.isChecked())
                        setConfirmationDialogHidden();
                }
            })
            .create();
        }
            break;

        default:
            break;
        }
        return super.onCreateDialog(id);
    }

    @Override
    @Deprecated
    protected void onPrepareDialog(int id, Dialog dialog) {
        switch (id) {
        case CONFIRMATION_DIALOG: 

            CheckBox checkBox = (CheckBox)dialog.findViewById(R.id.checkBox_DialogTranspConfirm);
            checkBox.setChecked(false);
            TextView textView = (TextView)dialog.findViewById(R.id.textView_DialogTranspConfirm);
            textView.setText(getString(R.string.you_use_service_for, Utils.getNumber()));


            break;

        default:
            break;
        }
        super.onPrepareDialog(id, dialog);
    }
}

它仅在 HTC One S 上崩溃,当我的应用程序从后台返回时,我设置了自定义视图:

.setView(mLoginConfirmView)

抛出:java.lang.IllegalStateException:指定的孩子已经有一个父母。您必须首先在孩子的父母上调用 removeView()。我决定添加以下检查:如果视图有父视图,我什么也不做:

@Override
    @Deprecated
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case CONFIRMATION_DIALOG: {

        ViewGroup parent = (ViewGroup) mLoginConfirmView.getParent();
            if (parent!=null)
                return super.onCreateDialog(id);

            return new AlertDialog.Builder(this)
            .setIcon(android.R.drawable.stat_sys_warning)
            .setTitle(R.string.app_name)
            .setView(mLoginConfirmView)
            .setPositiveButton(R.string.change, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                    dialog.dismiss();

                    showDialog(EXIT_PROGRESS_DIALOG);
                }
            })
            .setNegativeButton(R.string.go_ahead, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                    dialog.dismiss();

                    if (mCheckBoxLoginConfirm.isChecked())
                        setConfirmationDialogHidden();
                }
            })
            .create();
        }
            break;

        default:
            break;
        }
        return super.onCreateDialog(id);
    }

是正确的方式吗?我可以显示来自 onCreate() 的对话框吗?

4

1 回答 1

2

它将崩溃,因为活动的 UI 尚未准备好,活动尚未向用户显示,android 在其 onResume() 执行完成后显示屏幕,尝试显示来自 onResume() 的对话框

于 2012-09-20T08:50:12.347 回答