Android 在 Fragment 类中显示 ProgressDialog

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24825114/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:49:12  来源:igfitidea点击:

Show ProgressDialog in Fragment class

androidandroid-fragmentsprogressdialog

提问by Sini Inis

I am trying to show a ProgressDialogwithin a Fragmentclass. The following code just works within an Activityclass but not for Fragment. Can somebody please help me on this, why this ProgressDialogimplementaion just works within an Activityand not for a Fragment?

我试图ProgressDialog在一个Fragment班级中展示一个。以下代码仅适用于一个Activity类,但不适用于Fragment. 有人可以帮我解决这个问题,为什么这个ProgressDialog实现只适用于 aActivity而不是适用于 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();

    }
}

回答by M D

Try this in Fragment

试试这个 Fragment

 nDialog = new ProgressDialog(getActivity()); 

回答by navneet sharma

ProgressDialogtake Contextinput so use getActivity()in object creation.

ProgressDialog接受Context输入,以便getActivity()在对象创建中使用。

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