在asyncTask中显示Progressdialog

时间:2013-12-11 08:10:28

标签: android android-asynctask progressdialog

我有从Web服务获取结果的异步任务。我的班级我试图显示和隐藏进度对话框。但是在从Web服务获取结果的过程中,您会在log cat中看到一些错误。我认为问题在于我试图将结果置于异步模式,然后完成当前活动。因为我的异步任务是在构造函数中获取我的活动并使用它来显示ProgressDialog,所以在完成活动并尝试在异步任务中显示另一个进度对话框后,出现了这个问题。我该怎么办?

public class AsyncCallWs extends AsyncTask<String, Void, String> {

    private ProgressDialog dialog;
    public String methodName="";
    private WebService ws;
    private ArrayList<ServiceParam> paramsList;
    private boolean hasParams; 
    private Activity activity;
    private OnTaskCompleted listener;

    public AsyncCallWs(Activity activity,OnTaskCompleted listener,String methodName) {
        xLog.position();
        try {
            this.methodName = methodName;
            this.activity = activity;
            hasParams = false;
            this.listener = listener;
        } catch (Exception e) {
            xLog.error(e.getMessage());
        }
    }

    public AsyncCallWs(Activity activity,OnTaskCompleted listener,String methodName,ArrayList<ServiceParam> params) {
        xLog.position();
        try {
            this.methodName = methodName;
            this.activity = activity;
            this.paramsList = params;
            hasParams = true;
            this.listener = listener;
        } catch (Exception e) {
            xLog.error(e.getMessage());
        }
    }

    @Override
    protected void onPreExecute() {
        this.dialog = new ProgressDialog(activity);
        this.dialog.setMessage("Loading...");
        this.dialog.show();
    }

    @Override
    protected String doInBackground(String... params) {
        //Do something...
    }

    @Override
    protected void onPostExecute(String result) {
        xLog.position();

        if (this.dialog.isShowing()) {
            this.dialog.dismiss();
        }
        listener.onTaskCompleted(result,methodName);
        xLog.info("Output of current AsyncTask is:"+ result);

    }
}

logcat的:

12-11 08:08:58.808: E/WindowManager(26772): Activity com.example.shareholders.Login has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{40fc05a0 V.E..... R.....ID 0,0-168,114} that was originally added here
12-11 08:08:58.808: E/WindowManager(26772): android.view.WindowLeaked: Activity com.example.shareholders.Login has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{40fc05a0 V.E..... R.....ID 0,0-168,114} that was originally added here
12-11 08:08:58.808: E/WindowManager(26772):     at android.view.ViewRootImpl.<init>(ViewRootImpl.java:354)
12-11 08:08:58.808: E/WindowManager(26772):     at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:216)
12-11 08:08:58.808: E/WindowManager(26772):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
12-11 08:08:58.808: E/WindowManager(26772):     at android.app.Dialog.show(Dialog.java:281)
12-11 08:08:58.808: E/WindowManager(26772):     at ClassLibrary.AsyncCallWs.onPreExecute(AsyncCallWs.java:85)
12-11 08:08:58.808: E/WindowManager(26772):     at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
12-11 08:08:58.808: E/WindowManager(26772):     at android.os.AsyncTask.execute(AsyncTask.java:534)
12-11 08:08:58.808: E/WindowManager(26772):     at com.example.shareholders.Sync.doSync(Sync.java:307)
12-11 08:08:58.808: E/WindowManager(26772):     at com.example.shareholders.Login.clickHandler(Login.java:70)
12-11 08:08:58.808: E/WindowManager(26772):     at java.lang.reflect.Method.invokeNative(Native Method)
12-11 08:08:58.808: E/WindowManager(26772):     at java.lang.reflect.Method.invoke(Method.java:511)
12-11 08:08:58.808: E/WindowManager(26772):     at android.view.View$1.onClick(View.java:3594)
12-11 08:08:58.808: E/WindowManager(26772):     at android.view.View.performClick(View.java:4204)
12-11 08:08:58.808: E/WindowManager(26772):     at android.view.View$PerformClick.run(View.java:17355)
12-11 08:08:58.808: E/WindowManager(26772):     at android.os.Handler.handleCallback(Handler.java:725)
12-11 08:08:58.808: E/WindowManager(26772):     at android.os.Handler.dispatchMessage(Handler.java:92)
12-11 08:08:58.808: E/WindowManager(26772):     at android.os.Looper.loop(Looper.java:137)
12-11 08:08:58.808: E/WindowManager(26772):     at android.app.ActivityThread.main(ActivityThread.java:5041)
12-11 08:08:58.808: E/WindowManager(26772):     at java.lang.reflect.Method.invokeNative(Native Method)
12-11 08:08:58.808: E/WindowManager(26772):     at java.lang.reflect.Method.invoke(Method.java:511)
12-11 08:08:58.808: E/WindowManager(26772):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-11 08:08:58.808: E/WindowManager(26772):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-11 08:08:58.808: E/WindowManager(26772):     at dalvik.system.NativeStart.main(Native Method)
12-11 08:08:58.837: E/WindowManager(26772): Activity com.example.shareholders.Login has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{40fef4a8 V.E..... R.....ID 0,0-168,114} that was originally added here
12-11 08:08:58.837: E/WindowManager(26772): android.view.WindowLeaked: Activity com.example.shareholders.Login has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{40fef4a8 V.E..... R.....ID 0,0-168,114} that was originally added here
12-11 08:08:58.837: E/WindowManager(26772):     at android.view.ViewRootImpl.<init>(ViewRootImpl.java:354)
12-11 08:08:58.837: E/WindowManager(26772):     at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:216)
12-11 08:08:58.837: E/WindowManager(26772):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
12-11 08:08:58.837: E/WindowManager(26772):     at android.app.Dialog.show(Dialog.java:281)
12-11 08:08:58.837: E/WindowManager(26772):     at ClassLibrary.AsyncCallWs.onPreExecute(AsyncCallWs.java:85)
12-11 08:08:58.837: E/WindowManager(26772):     at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
12-11 08:08:58.837: E/WindowManager(26772):     at android.os.AsyncTask.execute(AsyncTask.java:534)
12-11 08:08:58.837: E/WindowManager(26772):     at com.example.shareholders.Sync.doSync(Sync.java:311)
12-11 08:08:58.837: E/WindowManager(26772):     at com.example.shareholders.Login.clickHandler(Login.java:70)
12-11 08:08:58.837: E/WindowManager(26772):     at java.lang.reflect.Method.invokeNative(Native Method)
12-11 08:08:58.837: E/WindowManager(26772):     at java.lang.reflect.Method.invoke(Method.java:511)
12-11 08:08:58.837: E/WindowManager(26772):     at android.view.View$1.onClick(View.java:3594)
12-11 08:08:58.837: E/WindowManager(26772):     at android.view.View.performClick(View.java:4204)
12-11 08:08:58.837: E/WindowManager(26772):     at android.view.View$PerformClick.run(View.java:17355)
12-11 08:08:58.837: E/WindowManager(26772):     at android.os.Handler.handleCallback(Handler.java:725)
12-11 08:08:58.837: E/WindowManager(26772):     at android.os.Handler.dispatchMessage(Handler.java:92)
12-11 08:08:58.837: E/WindowManager(26772):     at android.os.Looper.loop(Looper.java:137)
12-11 08:08:58.837: E/WindowManager(26772):     at android.app.ActivityThread.main(ActivityThread.java:5041)
12-11 08:08:58.837: E/WindowManager(26772):     at java.lang.reflect.Method.invokeNative(Native Method)
12-11 08:08:58.837: E/WindowManager(26772):     at java.lang.reflect.Method.invoke(Method.java:511)
12-11 08:08:58.837: E/WindowManager(26772):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-11 08:08:58.837: E/WindowManager(26772):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-11 08:08:58.837: E/WindowManager(26772):     at dalvik.system.NativeStart.main(Native Method)
12-11 08:08:58.867: E/WindowManager(26772): Activity com.example.shareholders.Login has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{40fb88b0 V.E..... R.....ID 0,0-168,114} that was originally added here
12-11 08:08:58.867: E/WindowManager(26772): android.view.WindowLeaked: Activity com.example.shareholders.Login has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{40fb88b0 V.E..... R.....ID 0,0-168,114} that was originally added here
12-11 08:08:58.867: E/WindowManager(26772):     at android.view.ViewRootImpl.<init>(ViewRootImpl.java:354)
12-11 08:08:58.867: E/WindowManager(26772):     at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:216)
12-11 08:08:58.867: E/WindowManager(26772):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
12-11 08:08:58.867: E/WindowManager(26772):     at android.app.Dialog.show(Dialog.java:281)
12-11 08:08:58.867: E/WindowManager(26772):     at ClassLibrary.AsyncCallWs.onPreExecute(AsyncCallWs.java:85)
12-11 08:08:58.867: E/WindowManager(26772):     at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
12-11 08:08:58.867: E/WindowManager(26772):     at android.os.AsyncTask.execute(AsyncTask.java:534)
12-11 08:08:58.867: E/WindowManager(26772):     at com.example.shareholders.Sync.doSync(Sync.java:316)
12-11 08:08:58.867: E/WindowManager(26772):     at com.example.shareholders.Login.clickHandler(Login.java:70)
12-11 08:08:58.867: E/WindowManager(26772):     at java.lang.reflect.Method.invokeNative(Native Method)
12-11 08:08:58.867: E/WindowManager(26772):     at java.lang.reflect.Method.invoke(Method.java:511)
12-11 08:08:58.867: E/WindowManager(26772):     at android.view.View$1.onClick(View.java:3594)
12-11 08:08:58.867: E/WindowManager(26772):     at android.view.View.performClick(View.java:4204)
12-11 08:08:58.867: E/WindowManager(26772):     at android.view.View$PerformClick.run(View.java:17355)
12-11 08:08:58.867: E/WindowManager(26772):     at android.os.Handler.handleCallback(Handler.java:725)
12-11 08:08:58.867: E/WindowManager(26772):     at android.os.Handler.dispatchMessage(Handler.java:92)
12-11 08:08:58.867: E/WindowManager(26772):     at android.os.Looper.loop(Looper.java:137)
12-11 08:08:58.867: E/WindowManager(26772):     at android.app.ActivityThread.main(ActivityThread.java:5041)
12-11 08:08:58.867: E/WindowManager(26772):     at java.lang.reflect.Method.invokeNative(Native Method)
12-11 08:08:58.867: E/WindowManager(26772):     at java.lang.reflect.Method.invoke(Method.java:511)
12-11 08:08:58.867: E/WindowManager(26772):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-11 08:08:58.867: E/WindowManager(26772):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-11 08:08:58.867: E/WindowManager(26772):     at dalvik.system.NativeStart.main(Native Method)
12-11 08:08:58.878: E/WindowManager(26772): Activity com.example.shareholders.Login has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{4102c668 V.E..... R.....ID 0,0-168,114} that was originally added here
12-11 08:08:58.878: E/WindowManager(26772): android.view.WindowLeaked: Activity com.example.shareholders.Login has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{4102c668 V.E..... R.....ID 0,0-168,114} that was originally added here
12-11 08:08:58.878: E/WindowManager(26772):     at android.view.ViewRootImpl.<init>(ViewRootImpl.java:354)
12-11 08:08:58.878: E/WindowManager(26772):     at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:216)
12-11 08:08:58.878: E/WindowManager(26772):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
12-11 08:08:58.878: E/WindowManager(26772):     at android.app.Dialog.show(Dialog.java:281)
12-11 08:08:58.878: E/WindowManager(26772):     at ClassLibrary.AsyncCallWs.onPreExecute(AsyncCallWs.java:85)
12-11 08:08:58.878: E/WindowManager(26772):     at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
12-11 08:08:58.878: E/WindowManager(26772):     at android.os.AsyncTask.execute(AsyncTask.java:534)
12-11 08:08:58.878: E/WindowManager(26772):     at com.example.shareholders.Sync.doSync(Sync.java:320)
12-11 08:08:58.878: E/WindowManager(26772):     at com.example.shareholders.Login.clickHandler(Login.java:70)
12-11 08:08:58.878: E/WindowManager(26772):     at java.lang.reflect.Method.invokeNative(Native Method)
12-11 08:08:58.878: E/WindowManager(26772):     at java.lang.reflect.Method.invoke(Method.java:511)
12-11 08:08:58.878: E/WindowManager(26772):     at android.view.View$1.onClick(View.java:3594)
12-11 08:08:58.878: E/WindowManager(26772):     at android.view.View.performClick(View.java:4204)
12-11 08:08:58.878: E/WindowManager(26772):     at android.view.View$PerformClick.run(View.java:17355)
12-11 08:08:58.878: E/WindowManager(26772):     at android.os.Handler.handleCallback(Handler.java:725)
12-11 08:08:58.878: E/WindowManager(26772):     at android.os.Handler.dispatchMessage(Handler.java:92)
12-11 08:08:58.878: E/WindowManager(26772):     at android.os.Looper.loop(Looper.java:137)
12-11 08:08:58.878: E/WindowManager(26772):     at android.app.ActivityThread.main(ActivityThread.java:5041)
12-11 08:08:58.878: E/WindowManager(26772):     at java.lang.reflect.Method.invokeNative(Native Method)
12-11 08:08:58.878: E/WindowManager(26772):     at java.lang.reflect.Method.invoke(Method.java:511)
12-11 08:08:58.878: E/WindowManager(26772):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-11 08:08:58.878: E/WindowManager(26772):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-11 08:08:58.878: E/WindowManager(26772):     at dalvik.system.NativeStart.main(Native Method)
12-11 08:08:58.898: E/WindowManager(26772): Activity com.example.shareholders.Login has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{4103cd98 V.E..... R.....ID 0,0-168,114} that was originally added here
12-11 08:08:58.898: E/WindowManager(26772): android.view.WindowLeaked: Activity com.example.shareholders.Login has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{4103cd98 V.E..... R.....ID 0,0-168,114} that was originally added here
12-11 08:08:58.898: E/WindowManager(26772):     at android.view.ViewRootImpl.<init>(ViewRootImpl.java:354)
12-11 08:08:58.898: E/WindowManager(26772):     at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:216)
12-11 08:08:58.898: E/WindowManager(26772):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
12-11 08:08:58.898: E/WindowManager(26772):     at android.app.Dialog.show(Dialog.java:281)
12-11 08:08:58.898: E/WindowManager(26772):     at ClassLibrary.AsyncCallWs.onPreExecute(AsyncCallWs.java:85)
12-11 08:08:58.898: E/WindowManager(26772):     at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
12-11 08:08:58.898: E/WindowManager(26772):     at android.os.AsyncTask.execute(AsyncTask.java:534)
12-11 08:08:58.898: E/WindowManager(26772):     at com.example.shareholders.Sync.doSync(Sync.java:325)
12-11 08:08:58.898: E/WindowManager(26772):     at com.example.shareholders.Login.clickHandler(Login.java:70)
12-11 08:08:58.898: E/WindowManager(26772):     at java.lang.reflect.Method.invokeNative(Native Method)
12-11 08:08:58.898: E/WindowManager(26772):     at java.lang.reflect.Method.invoke(Method.java:511)
12-11 08:08:58.898: E/WindowManager(26772):     at android.view.View$1.onClick(View.java:3594)
12-11 08:08:58.898: E/WindowManager(26772):     at android.view.View.performClick(View.java:4204)
12-11 08:08:58.898: E/WindowManager(26772):     at android.view.View$PerformClick.run(View.java:17355)
12-11 08:08:58.898: E/WindowManager(26772):     at android.os.Handler.handleCallback(Handler.java:725)
12-11 08:08:58.898: E/WindowManager(26772):     at android.os.Handler.dispatchMessage(Handler.java:92)
12-11 08:08:58.898: E/WindowManager(26772):     at android.os.Looper.loop(Looper.java:137)
12-11 08:08:58.898: E/WindowManager(26772):     at android.app.ActivityThread.main(ActivityThread.java:5041)
12-11 08:08:58.898: E/WindowManager(26772):     at java.lang.reflect.Method.invokeNative(Native Method)
12-11 08:08:58.898: E/WindowManager(26772):     at java.lang.reflect.Method.invoke(Method.java:511)
12-11 08:08:58.898: E/WindowManager(26772):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-11 08:08:58.898: E/WindowManager(26772):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-11 08:08:58.898: E/WindowManager(26772):     at dalvik.system.NativeStart.main(Native Method)


12-11 08:08:59.118: D/AndroidRuntime(26772): Shutting down VM
12-11 08:08:59.138: W/dalvikvm(26772): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
12-11 08:08:59.167: E/AndroidRuntime(26772): FATAL EXCEPTION: main
12-11 08:08:59.167: E/AndroidRuntime(26772): java.lang.IllegalArgumentException: View not attached to window manager
12-11 08:08:59.167: E/AndroidRuntime(26772):    at android.view.WindowManagerGlobal.findViewLocked(WindowManagerGlobal.java:383)
12-11 08:08:59.167: E/AndroidRuntime(26772):    at android.view.WindowManagerGlobal.removeView(WindowManagerGlobal.java:285)
12-11 08:08:59.167: E/AndroidRuntime(26772):    at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:79)
12-11 08:08:59.167: E/AndroidRuntime(26772):    at android.app.Dialog.dismissDialog(Dialog.java:323)
12-11 08:08:59.167: E/AndroidRuntime(26772):    at android.app.Dialog.dismiss(Dialog.java:306)
12-11 08:08:59.167: E/AndroidRuntime(26772):    at ClassLibrary.AsyncCallWs.onPostExecute(AsyncCallWs.java:113)
12-11 08:08:59.167: E/AndroidRuntime(26772):    at ClassLibrary.AsyncCallWs.onPostExecute(AsyncCallWs.java:1)
12-11 08:08:59.167: E/AndroidRuntime(26772):    at android.os.AsyncTask.finish(AsyncTask.java:631)
12-11 08:08:59.167: E/AndroidRuntime(26772):    at android.os.AsyncTask.access$600(AsyncTask.java:177)
12-11 08:08:59.167: E/AndroidRuntime(26772):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
12-11 08:08:59.167: E/AndroidRuntime(26772):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-11 08:08:59.167: E/AndroidRuntime(26772):    at android.os.Looper.loop(Looper.java:137)
12-11 08:08:59.167: E/AndroidRuntime(26772):    at android.app.ActivityThread.main(ActivityThread.java:5041)
12-11 08:08:59.167: E/AndroidRuntime(26772):    at java.lang.reflect.Method.invokeNative(Native Method)
12-11 08:08:59.167: E/AndroidRuntime(26772):    at java.lang.reflect.Method.invoke(Method.java:511)
12-11 08:08:59.167: E/AndroidRuntime(26772):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-11 08:08:59.167: E/AndroidRuntime(26772):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-11 08:08:59.167: E/AndroidRuntime(26772):    at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

在完成this.dialog.dismiss();

之前,您需要致电Activity

答案 1 :(得分:0)

您可以使用无头片段并在其中进行异步处理,而不是在活动内部。

这里有一个教程: http://techbandhu.wordpress.com/2013/07/02/android-headless-fragment/