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