这是我的MainActivity,我想要在AsyncTask类中处理进度对话框,但是将上下文传递给asyncTask似乎无法正常工作并且应用程序崩溃时出现NullPointerException错误,如果我尝试在MainActivity中设置progressDialog,然后获取引用在使用mContext.progress的AsyncTask中,应用程序因WindowLeaked错误而崩溃。
public class MainActivity extends Activity{
ConnectionHandler ch;
SearchForCard mTask = null;
String finalUrl = "";
Button searchBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ch = new ConnectionHandler(this);
searchBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mTask = ch.new SearchForCard();
mTask.execute(finalUrl);
}
}
});
}
在我的ConnectionHandler类中,我希望为我必须做的每一件事创建单独的asynctask,请不要在MainActivity中为内部Asynctask提供解决方案,这不是我想要做的事情
public class ConnectionHandler{
private MainActivity mContext;
Handler mHandler = new Handler();
Document doc = null;
Elements title = null;
String response = null;
ProgressDialog progress;
public ConnectionHandler(MainActivity ctx)
{
mContext = ctx;
progress = new ProgressDialog(mContext); <--APPLICATION CRASH HERE WITH DEBUGGER
progress.setTitle("loading");
progress.setIndeterminate(false);
}
public class SearchForCard extends AsyncTask<String, Void, Boolean> {
@Override
protected void onPreExecute() {
progress.show();
super.onPreExecute();
}
@Override
protected Boolean doInBackground(String... url) {
try{
doc = Jsoup.connect(url[0]).get();
Log.d("RESPONSE",doc.toString());
} catch (Exception e){
e.printStackTrace();
}
return false;
}
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
progress.dismiss();
}
}
}
我做错了什么?感谢
这是错误:
02-08 12:40:28.275: E/WindowManager(3574): Activity com.pagiodevelop.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@5371c7b4 that was originally added here
02-08 12:40:28.275: E/WindowManager(3574): android.view.WindowLeaked: Activity com.pagiodevelop.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@5371c7b4 that was originally added here
02-08 12:40:28.275: E/WindowManager(3574): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:374)
02-08 12:40:28.275: E/WindowManager(3574): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:292)
02-08 12:40:28.275: E/WindowManager(3574): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
02-08 12:40:28.275: E/WindowManager(3574): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
02-08 12:40:28.275: E/WindowManager(3574): at android.view.Window$LocalWindowManager.addView(Window.java:547)
02-08 12:40:28.275: E/WindowManager(3574): at android.app.Dialog.show(Dialog.java:277)
02-08 12:40:28.275: E/WindowManager(3574): at com.pagiodevelop.ConnectionHandler$SearchForCard.onPreExecute(ConnectionHandler.java:40)
02-08 12:40:28.275: E/WindowManager(3574): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
02-08 12:40:28.275: E/WindowManager(3574): at android.os.AsyncTask.execute(AsyncTask.java:534)
02-08 12:40:28.275: E/WindowManager(3574): at com.pagiodevelop.MainActivity$1.onClick(MainActivity.java:50)
02-08 12:40:28.275: E/WindowManager(3574): at android.view.View.performClick(View.java:4084)
02-08 12:40:28.275: E/WindowManager(3574): at android.view.View$PerformClick.run(View.java:16966)
02-08 12:40:28.275: E/WindowManager(3574): at android.os.Handler.handleCallback(Handler.java:615)
02-08 12:40:28.275: E/WindowManager(3574): at android.os.Handler.dispatchMessage(Handler.java:92)
02-08 12:40:28.275: E/WindowManager(3574): at android.os.Looper.loop(Looper.java:137)
02-08 12:40:28.275: E/WindowManager(3574): at android.app.ActivityThread.main(ActivityThread.java:4745)
02-08 12:40:28.275: E/WindowManager(3574): at java.lang.reflect.Method.invokeNative(Native Method)
02-08 12:40:28.275: E/WindowManager(3574): at java.lang.reflect.Method.invoke(Method.java:511)
02-08 12:40:28.275: E/WindowManager(3574): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
02-08 12:40:28.275: E/WindowManager(3574): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-08 12:40:28.275: E/WindowManager(3574): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
使用进度对话框和异步任务时,这应该是正确的解决方案
public static class SearchForCard extends AsyncTask<String, Void, Boolean> {
private Context mContext;
private ProgressDialog mProgress;
public SearchForCard (Context context) {
mContext = context;
}
@Override
protected void onPreExecute() {
mProgress = new ProgressDialog(mContext);
//customize the progress dialog here
mProgress.setCancelable(false);
mProgress.show();
}
@Override
protected Boolean doInBackground(String... url) {
//do your background work and return something
return false;
}
@Override
protected void onPostExecute(Boolean result) {
if (mProgress != null && mProgress.isShowing())
mProgress.dismiss();
//evaluate result
}
}