这是我的代码:
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()的对话框吗?
答案 0 :(得分:2)
它会崩溃,因为活动的UI尚未准备好,活动尚未显示给用户,android在onResume()执行完成后显示屏幕,尝试从onResume()显示对话框