[英]External AsyncTask class with ProgressDialog [Update: and returning back?]
**Updated: (See below)**I have been looking around for couple of days and can't find a straight answer to this. **更新:(见下文)**我一直在寻找几天,但无法找到答案。 Some say it possible to some say to accomplish some say it's not.
有人说有可能有人说完成一些说它不是。 I am getting crazy on this.
我对此感到疯狂。
What I want is just to have the AsyncTaskTask showing a progressbar an external class. 我想要的只是让AsyncTaskTask显示进度条和外部类。 To do this I am passing the context as you can see in the main class.
要做到这一点,我正在传递上下文,你可以在主类中看到。 But whatever I try I get
NullPointerException
. 但无论我尝试什么,我都会得到
NullPointerException
。
Working code examples is appreciated. 工作代码示例表示赞赏。 Thank you
谢谢
Using Android 2.2 by the way. 顺便使用Android 2.2。
main: 主要:
import android.app.Activity;
import android.os.Bundle;
public class AsyncDemo extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new AsyncClass(this).execute();
}
}
AsyncClass.java AsyncClass.java
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.SystemClock;
public class AsyncClass extends AsyncTask<Void, String, Void> {
private Context context;
ProgressDialog dialog = new ProgressDialog(context);
public AsyncClass(Context cxt) {
context = cxt;
}
@Override
protected void onPreExecute() {
dialog.setTitle("Please wait");
dialog.show();
}
@Override
protected Void doInBackground(Void... unused) {
SystemClock.sleep(2000);
return (null);
}
@Override
protected void onPostExecute(Void unused) {
dialog.dismiss();
}
}
Update: I have a follow up question: Using the above code, is it possible to return a value from the onPostExecute method back to the main class, somehow? 更新:我有一个跟进问题:使用上面的代码,是否有可能从onPostExecute方法返回一个值回到主类,不知何故? (Sorry about beeing noobish) I have tried something like this:
(对不起,因为我正在尝试这样的事情:
String result = new AsyncClass(this).execute();
and then a method that return back a string. 然后是一个返回字符串的方法。 But I can't do that because i got:
但我做不到,因为我得到了:
Type mismatch: cannot convert from AsyncTask<String,Void,Void> to String
What can I do to tackle this problem? 我该怎么做才能解决这个问题? Thanks.
谢谢。
You were creating the ProgressDialog
with a null context. 您正在使用空上下文创建
ProgressDialog
。 The following code worked for me. 以下代码对我有用。
public class AsyncClass extends AsyncTask<Void, String, Void> {
private Context context;
ProgressDialog dialog;
public AsyncClass(Context cxt) {
context = cxt;
dialog = new ProgressDialog(context);
}
@Override
protected void onPreExecute() {
dialog.setTitle("Please wait");
dialog.show();
}
@Override
protected Void doInBackground(Void... unused) {
SystemClock.sleep(2000);
return (null);
}
@Override
protected void onPostExecute(Void unused) {
dialog.dismiss();
}
}
ok this is what I did as i was using fragments. 好吧,这就是我在使用片段时所做的。 This is how you call the AsyncTask inside a fragment:
这就是你在片段中调用AsyncTask的方法:
String result=new AsyncClass(getActivity()).execute();
and this is how my AsyncTask outer class looks like: 这就是我的AsyncTask外部类的样子:
public class AsyncClass extends AsyncTask<Void, Void, String> {
ProgressDialog pdialog;
public AsyncClass(Context context) {
pdialog = new ProgressDialog(context);
}
@Override
protected void onPreExecute() {
pdialog.setIndeterminate(true);
pdialog.setCancelable(false);
pdialog.setTitle("Loading Feed..");
pdialog.setMessage("Please wait.");pdialog.show();
}
@Override
protected String doInBackground(Void... params) {
String result=null;
//do your task here and generate result String
return result;
}
@Override
protected void onPostExecute(String result) {
if(pdialog.isShowing())
pdialog.dismiss();
}
}
@ SSZero thanks great answer, helped a lot. @ SSZero感谢很棒的答案,帮了很多忙。
I would like to answer that follow up question ie 我想回答一下后续问题即
I have a follow up question: Using the above code, is it possible to return a value from the onPostExecute method back to the main class, somehow?
我有一个跟进问题:使用上面的代码,是否可以以某种方式将onPostExecute方法中的值返回给主类? (Sorry about beeing noobish) I have tried something like this:
(对不起,因为我正在尝试这样的事情:
String result = new AsyncClass(this).execute();
I did this in my code it worked. 我在我的代码中做到了这一点。
AsyncClass ac=new AsyncClass();
ac.execute("");
String rslt=ac.get();
Call this code wherever you want to. 在任何地方调用此代码。
public class AsynchClass extends AsyncTask <String,Integer,String>
{
public String result=null;
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
}
protected String doInBackground(String... params)
{
// Do all your background task here
return result;
}
@Override
protected void onProgressUpdate(Integer... values) {
}
protected void onPostExecute(String result) {
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.