在Fragment类中显示ProgressDialog

时间:2014-07-18 12:31:20

标签: android android-fragments progressdialog

我想在ProgressDialog课程中展示Fragment。以下代码仅适用于Activity类,但不适用于Fragment。有人可以帮我解决这个问题,为什么这个ProgressDialog实现只适用于Activity而不适用于Fragment

private class ProcessUpdateProfile extends
        AsyncTask<String, String, JSONObject> {

    private ProgressDialog nDialog;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        nDialog = new ProgressDialog(PFragment.this); //Here I get an error: The constructor ProgressDialog(PFragment) is undefined
        nDialog.setMessage("Loading..");
        nDialog.setTitle("Checking Network");
        nDialog.setIndeterminate(false);
        nDialog.setCancelable(true);
        nDialog.show();

    }
}

2 个答案:

答案 0 :(得分:63)

Fragment

中试试
 nDialog = new ProgressDialog(getActivity()); 

答案 1 :(得分:12)

ProgressDialog接受Context输入,因此在对象创建中使用getActivity()

ProgressDialog dialog = ProgressDialog.show(getActivity(), "Loading...", "Please wait...", true);