[英]can't show dialog in fragment
我想在片段中顯示一個進度對話框,但是它拋出一個異常,指出必須在設置內容之前調用requestFeature()。 這是我的代碼:
public class TestFragment extends Fragment {
private Dialog _pDialog;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
_pDialog = new ProgressDialog(this.getActivity());
_pDialog.setContentView(R.layout.myprogressdialog);
_pDialog.show();
}
}
它在.show()
處引發異常。 任何幫助,將不勝感激。
編輯:我為自己犯的錯誤感到很愚蠢,將_pDialog聲明為Dialog,然后初始化為PogressDialog,這導致引發異常,我將其更改為:
_pDialog = new Dialog(this.getActivity());
現在工作正常。
在OncreateView
或Onactivitycreated
執行此Onactivitycreated
。
你可以用
_pDialog = new ProgressDialog(getActivity());
創建自定義對話框使用此
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(inflater.inflate(R.layout.customdialogui, null))
// Add action buttons
.setPositiveButton(R.string.signin, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
// do something..
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
LoginDialogFragment.this.getDialog().cancel();
}
});
return builder.create();
檢查此鏈接以獲取有關對話框的更多信息:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.