今天编写ProgressDialog对话框时创建窗体时出现以下错误:
错误代码如下:
public void showProgressDialog(String message,boolean isCancleable){
if(mProgressDialog==null){
mProgressDialog=new ProgressDialog(mContext);
mView_progress=LayoutInflater.from(this).inflate(R.layout.progress_dialog, null);
mTextView_Content=(TextView) mView_progress.findViewById(R.id.progress_dialog_content);
WindowManager.LayoutParams mWindowManager=getWindow().getAttributes();
mWindowManager.height = ViewGroup.LayoutParams.MATCH_PARENT;
mWindowManager.width = ViewGroup.LayoutParams.MATCH_PARENT;
getWindow().setAttributes(mWindowManager);
mTextView_Content.setText(message);
mProgressDialog.setCancelable(isCancleable);
}else{
mTextView_Content.setText(message);
mProgressDialog.setCancelable(isCancleable);
}
mProgressDialog.show();
mProgressDialog.setContentView(mView_progress);
}
由于ProgressDialog继承于AlertDialog,查找网上资料发现错误是由于在AlertDialog onCreate()方法中请求设置 mWindow.requestFeature(Window.FEATURE_NO_TITLE);
倘若再设置ContentView()时便会造成如上错误。在SetContentView之前调用show()方法便能避免如上的错误。附上AlertDialog中调用 mAlert.installContent();方法中的部分源代码:
public void installContent() {
/* We use a custom title so never request a window title */
mWindow.requestFeature(Window.FEATURE_NO_TITLE);
if (mView == null) {
mWindow.setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,
WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
}
mWindow.setContentView(com.android.internal.R.layout.alert_dialog);
setupView();
}
public void setTitle(CharSequence title) {
mTitle = title;
if (mTitleView != null) {
mTitleView.setText(title);
}
}