Show ProgressDialog in Fragment class

十年热恋 提交于 2019-11-30 13:08:43

问题


I am trying to show a ProgressDialog within a Fragment class. The following code just works within an Activity class but not for Fragment. Can somebody please help me on this, why this ProgressDialog implementaion just works within an Activity and not for a 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();

    }
}

回答1:


Try this in Fragment

 nDialog = new ProgressDialog(getActivity()); 



回答2:


ProgressDialog take Context input so use getActivity() in object creation.

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


来源:https://stackoverflow.com/questions/24825114/show-progressdialog-in-fragment-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!