0

我想在片段中顯示進度對話框,但它會引發一個異常,指出在設置內容之前必須調用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被宣佈爲對話框,然後初始化爲PogressDialog錯誤很愚蠢的,這是造成異常被拋出,我把它改成這樣:

_pDialog = new Dialog(this.getActivity()); 

現在它工作的很好。

回答

0

做,在OncreateViewOnactivitycreated ..

你可以使用

_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(); 

檢查此鏈接瞭解對話的更多信息:

http://developer.android.com/guide/topics/ui/dialogs.html