无法在片段中显示对话框

时间:2014-09-27 15:20:38

标签: android android-fragments progressdialog android-fragmentactivity

我想在片段中显示进度对话框,但它会抛出一个异常,指出必须在设置内容之前调用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());

现在工作正常。

1 个答案:

答案 0 :(得分:0)

OncreateViewOnactivitycreated ..

中执行此操作

你可以使用

_pDialog = new ProgressDialog(getActivity());

创建自定义Dialog使用此

 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