Android 未显示从DialogFragment调用的AsyncTask中的ProgressDialog
我正在DialogFragment内部执行一个AsyncTask,但是在doInBackground期间进度条没有显示。代码如下:Android 未显示从DialogFragment调用的AsyncTask中的ProgressDialog,android,android-asynctask,progress-bar,progressdialog,Android,Android Asynctask,Progress Bar,Progressdialog,我正在DialogFragment内部执行一个AsyncTask,但是在doInBackground期间进度条没有显示。代码如下: public class GetCustomerSoapAsync extends AsyncTask<Void, Void, String> { ProgressDialog prg; ActionBarActivity activity; GetResponseFromGetCustomer listener; public Ge
public class GetCustomerSoapAsync extends AsyncTask<Void, Void, String>
{
ProgressDialog prg;
ActionBarActivity activity;
GetResponseFromGetCustomer listener;
public GetCustomerSoapAsync(ActionBarActivity activity, GetResponseFromGetCustomer listener)
{
this.activity = activity;
this.listener = listener;
prg = new ProgressDialog(activity);
prg.setMessage("Lütfen Bekleyin");
Log.i("ED","Progress will be shown");
prg.show();
}
@Override protected String doInBackground(Void... params)
{
//some stuff
}
@Override protected void onPostExecute(String s)
{
listener.getResponseFromGetCustomer(s);
if (prg.isShowing())
{
prg.dismiss();
}
}
最后,可能是由于任务流有缺陷或其他原因,回调无法完成其工作,并且响应未设置为返回AsyncTask的值
谢谢你的帮助 您应该使用
onPreExecute
:
class Task1 extends AsyncTask<Object, Void, String []> {
/** The Progress dialog. */
private final ProgressDialog dialog = new ProgressDialog(YourActivity.this);
/**
* Set the Progress dialog.
*/
protected void onPreExecute()
{
super.onPreExecute();
this.dialog.setMessage("Loading...");
this.dialog.show();
dialog.setCanceledOnTouchOutside(false);
}
protected String[] doInBackground(Object... params) {
///
}
protected void onPostExecute(String [] result)
{
super.onPostExecute(result);
this.dialog.dismiss();
}
}
希望有帮助!:) 您应该使用
onPreExecute
:
class Task1 extends AsyncTask<Object, Void, String []> {
/** The Progress dialog. */
private final ProgressDialog dialog = new ProgressDialog(YourActivity.this);
/**
* Set the Progress dialog.
*/
protected void onPreExecute()
{
super.onPreExecute();
this.dialog.setMessage("Loading...");
this.dialog.show();
dialog.setCanceledOnTouchOutside(false);
}
protected String[] doInBackground(Object... params) {
///
}
protected void onPostExecute(String [] result)
{
super.onPostExecute(result);
this.dialog.dismiss();
}
}
希望有帮助!:) 您应该使用
onPreExecute
:
class Task1 extends AsyncTask<Object, Void, String []> {
/** The Progress dialog. */
private final ProgressDialog dialog = new ProgressDialog(YourActivity.this);
/**
* Set the Progress dialog.
*/
protected void onPreExecute()
{
super.onPreExecute();
this.dialog.setMessage("Loading...");
this.dialog.show();
dialog.setCanceledOnTouchOutside(false);
}
protected String[] doInBackground(Object... params) {
///
}
protected void onPostExecute(String [] result)
{
super.onPostExecute(result);
this.dialog.dismiss();
}
}
希望有帮助!:) 您应该使用
onPreExecute
:
class Task1 extends AsyncTask<Object, Void, String []> {
/** The Progress dialog. */
private final ProgressDialog dialog = new ProgressDialog(YourActivity.this);
/**
* Set the Progress dialog.
*/
protected void onPreExecute()
{
super.onPreExecute();
this.dialog.setMessage("Loading...");
this.dialog.show();
dialog.setCanceledOnTouchOutside(false);
}
protected String[] doInBackground(Object... params) {
///
}
protected void onPostExecute(String [] result)
{
super.onPostExecute(result);
this.dialog.dismiss();
}
}
希望有帮助!:) 您只需要在UI线程上操作ProgressDialog asynctask上的构造函数提供了足够的灵活性,可以将任务放入它自己的类中。注意:在自定义AsyncTask的构造函数中初始化的任何字段都必须利用java final关键字,以便字段变量自动为空,以便进行垃圾回收 需要在onPreExecute()中调用Solution ProgressDialog代码,此时任务仍在UI线程上
public class GetCustomerSoapAsync extends AsyncTask<Void, Void, String>
{
ProgressDialog prg;
// use final for fields initialized in a constructor.
final ActionBarActivity activity;
final GetResponseFromGetCustomer listener;
您只需要在UI线程上操作ProgressDialog asynctask上的构造函数提供了足够的灵活性,可以将任务放入它自己的类中。注意:在自定义AsyncTask的构造函数中初始化的任何字段都必须利用java final关键字,以便字段变量自动为空,以便进行垃圾回收 需要在onPreExecute()中调用Solution ProgressDialog代码,此时任务仍在UI线程上
public class GetCustomerSoapAsync extends AsyncTask<Void, Void, String>
{
ProgressDialog prg;
// use final for fields initialized in a constructor.
final ActionBarActivity activity;
final GetResponseFromGetCustomer listener;
您只需要在UI线程上操作ProgressDialog asynctask上的构造函数提供了足够的灵活性,可以将任务放入它自己的类中。注意:在自定义AsyncTask的构造函数中初始化的任何字段都必须利用java final关键字,以便字段变量自动为空,以便进行垃圾回收 需要在onPreExecute()中调用Solution ProgressDialog代码,此时任务仍在UI线程上
public class GetCustomerSoapAsync extends AsyncTask<Void, Void, String>
{
ProgressDialog prg;
// use final for fields initialized in a constructor.
final ActionBarActivity activity;
final GetResponseFromGetCustomer listener;
您只需要在UI线程上操作ProgressDialog asynctask上的构造函数提供了足够的灵活性,可以将任务放入它自己的类中。注意:在自定义AsyncTask的构造函数中初始化的任何字段都必须利用java final关键字,以便字段变量自动为空,以便进行垃圾回收 需要在onPreExecute()中调用Solution ProgressDialog代码,此时任务仍在UI线程上
public class GetCustomerSoapAsync extends AsyncTask<Void, Void, String>
{
ProgressDialog prg;
// use final for fields initialized in a constructor.
final ActionBarActivity activity;
final GetResponseFromGetCustomer listener;
好的,我认为问题是,在执行Aysnch任务之前,您必须在布局中声明用户可见的PD 例如:
//Declare the pd here. Pd private to class.
private ProgressDialog pd;
builder.setMessage("Numaranız doğru mu?\n" + "0" + number)
.setPositiveButton(R.string.yes,
new DialogInterface.OnClickListener()
{
....
pd = ProgressDialog.show(getActivity(), "",
"Your Message Here!!!", false);
// Now using a modified constructor call your execute function
//Previous parametes + pd
GetCustomerSoapAsync gCSA = new GetCustomerSoapAsync(...,...,pd);
getCustomerSoapAsync.execute();
}
然后在异步类中:
public class GetCustomerSoapAsync extends AsyncTask<Void, Void, String>
{
ProgressDialog prg;
ActionBarActivity activity;
GetResponseFromGetCustomer listener;
public GetCustomerSoapAsync(ActionBarActivity activity, GetResponseFromGetCustomer listener,ProgressDialog pd)
ProgressDialog prg;
{
this.activity = activity;
this.listener = listener;
this.prg = pd;
}
@Override protected String doInBackground(Void... params)
{
//some stuff
}
@Override protected void onPostExecute(String s)
{
listener.getResponseFromGetCustomer(s);
if (prg.isShowing())
{
prg.dismiss();
}
}
公共类GetCustomerSoapAsync扩展异步任务
{
prg;
活动性活动;
GetResponseFromGetCustomer侦听器;
公共GetCustomerSoapAsync(ActionBarActivity活动、GetResponseFromGetCustomer侦听器、ProgressDialog pd)
prg;
{
这个。活动=活动;
this.listener=listener;
这个.prg=pd;
}
@覆盖受保护字符串doInBackground(无效…参数)
{
//一些东西
}
@重写受保护的void onPostExecute(字符串s)
{
listener.getResponseFromGetCustomer;
if(prg.isShowing())
{
prg.discouse();
}
}
Ps:我不知道您是否使用片段作为实现,但如果您使用片段,您必须通过rootview在片段中的onclick函数中调用相同的pd,否则您可能会在一个从未显示过的Progress对话框上调用函数。好的,我认为问题是您必须在布局vi中声明pd在执行Aysnch任务之前,用户可以访问 例如:
//Declare the pd here. Pd private to class.
private ProgressDialog pd;
builder.setMessage("Numaranız doğru mu?\n" + "0" + number)
.setPositiveButton(R.string.yes,
new DialogInterface.OnClickListener()
{
....
pd = ProgressDialog.show(getActivity(), "",
"Your Message Here!!!", false);
// Now using a modified constructor call your execute function
//Previous parametes + pd
GetCustomerSoapAsync gCSA = new GetCustomerSoapAsync(...,...,pd);
getCustomerSoapAsync.execute();
}
然后在异步类中:
public class GetCustomerSoapAsync extends AsyncTask<Void, Void, String>
{
ProgressDialog prg;
ActionBarActivity activity;
GetResponseFromGetCustomer listener;
public GetCustomerSoapAsync(ActionBarActivity activity, GetResponseFromGetCustomer listener,ProgressDialog pd)
ProgressDialog prg;
{
this.activity = activity;
this.listener = listener;
this.prg = pd;
}
@Override protected String doInBackground(Void... params)
{
//some stuff
}
@Override protected void onPostExecute(String s)
{
listener.getResponseFromGetCustomer(s);
if (prg.isShowing())
{
prg.dismiss();
}
}
公共类GetCustomerSoapAsync扩展异步任务
{
prg;
活动性活动;
GetResponseFromGetCustomer侦听器;
公共GetCustomerSoapAsync(ActionBarActivity活动、GetResponseFromGetCustomer侦听器、ProgressDialog pd)
prg;
{
这个。活动=活动;
this.listener=listener;
这个.prg=pd;
}
@覆盖受保护字符串doInBackground(无效…参数)
{
//一些东西
}
@重写受保护的void onPostExecute(字符串s)
{
listener.getResponseFromGetCustomer;
if(prg.isShowing())
{
prg.discouse();
}
}
Ps:我不知道您是否使用片段作为实现,但如果您使用片段,您必须通过rootview在片段中的onclick函数中调用相同的pd,否则您可能会在一个从未显示过的Progress对话框上调用函数。好的,我认为问题是您必须在布局vi中声明pd在执行Aysnch任务之前,用户可以访问 例如:
//Declare the pd here. Pd private to class.
private ProgressDialog pd;
builder.setMessage("Numaranız doğru mu?\n" + "0" + number)
.setPositiveButton(R.string.yes,
new DialogInterface.OnClickListener()
{
....
pd = ProgressDialog.show(getActivity(), "",
"Your Message Here!!!", false);
// Now using a modified constructor call your execute function
//Previous parametes + pd
GetCustomerSoapAsync gCSA = new GetCustomerSoapAsync(...,...,pd);
getCustomerSoapAsync.execute();
}
然后在异步类中:
public class GetCustomerSoapAsync extends AsyncTask<Void, Void, String>
{
ProgressDialog prg;
ActionBarActivity activity;
GetResponseFromGetCustomer listener;
public GetCustomerSoapAsync(ActionBarActivity activity, GetResponseFromGetCustomer listener,ProgressDialog pd)
ProgressDialog prg;
{
this.activity = activity;
this.listener = listener;
this.prg = pd;
}
@Override protected String doInBackground(Void... params)
{
//some stuff
}
@Override protected void onPostExecute(String s)
{
listener.getResponseFromGetCustomer(s);
if (prg.isShowing())
{
prg.dismiss();
}
}
公共类GetCustomerSoapAsync扩展异步任务
{
prg;
活动性活动;
GetResponseFromGetCustomer侦听器;
公共GetCustomerSoapAsync(ActionBarActivity活动、GetResponseFromGetCustomer侦听器、ProgressDialog pd)
prg;
{
这个。活动=活动;
this.listener=listener;
这个.prg=pd;
}
@覆盖受保护字符串doInBackground(无效…参数)
{
//一些东西
}
@重写受保护的void onPostExecute(字符串s)
{
listener.getResponseFromGetCustomer;
if(prg.isShowing())
{
prg.discouse();
}
}
附言:我不知道你是不是在用碎片作为