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
Show ProgressDialog in Fragment class
提问by Sini Inis
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
?
我试图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
ProgressDialog
take Context
input so use getActivity()
in object creation.
ProgressDialog
接受Context
输入,以便getActivity()
在对象创建中使用。
ProgressDialog dialog = ProgressDialog.show(getActivity(), "Loading...", "Please wait...", true);