1

此调用在以下位置完成onCreate

new retreive().execute();

该类asyncTask如下所示:

class retreive extends AsyncTask<Void,Void,Void>{
    ProgressDialog dialog = new ProgressDialog(busDisp.this);

    @Override
    protected void onPreExecute(){
        this.dialog.setMessage("Retreiving Bus Routes...");
        this.dialog.show();
        this.dialog.setCancelable(false);
    }

    @Override
    protected Void doInBackground(Void... arg0) {
        busRouteJsonParsing();
        this.dialog.dismiss();
        return null;
    }

    @Override
    protected void onPostExecute(Void voids){
            setContentView(R.layout.buses);
            stop1.setText(Integer.valueOf(stopsCtr[0]).toString()+" stops");
            stop2.setText(Integer.valueOf(stopsCtr[1]).toString()+" stops");
            stop3.setText(Integer.valueOf(stopsCtr[2]).toString()+" stops");
            stop4.setText(Integer.valueOf(stopsCtr[3]).toString()+" stops");
            dur1.setText(dur[0]);
            dur2.setText(dur[1]);
            dur3.setText(dur[2]);
            dur4.setText(dur[3]);
            this.dialog.dismiss();
    }

onClicks在这之后有8个PostExecute

这是日志:

12-20 19:55:57.701: E/WindowManager(982): Activity com.example.Try1.busDisp has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@418b7ad8 that was originally added here
12-20 19:55:57.701: E/WindowManager(982): android.view.WindowLeaked: Activity com.example.Try1.busDisp has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@418b7ad8 that was originally added here
12-20 19:55:57.701: E/WindowManager(982):   at android.view.ViewRootImpl.<init>(ViewRootImpl.java:344)
12-20 19:55:57.701: E/WindowManager(982):   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:267)
12-20 19:55:57.701: E/WindowManager(982):   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215)
12-20 19:55:57.701: E/WindowManager(982):   at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:140)
12-20 19:55:57.701: E/WindowManager(982):   at android.view.Window$LocalWindowManager.addView(Window.java:537)
12-20 19:55:57.701: E/WindowManager(982):   at android.app.Dialog.show(Dialog.java:278)
12-20 19:55:57.701: E/WindowManager(982):   at com.example.Try1.busDisp$retreive.onPreExecute(busDisp.java:249)
12-20 19:55:57.701: E/WindowManager(982):   at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:561)
12-20 19:55:57.701: E/WindowManager(982):   at android.os.AsyncTask.execute(AsyncTask.java:511)
12-20 19:55:57.701: E/WindowManager(982):   at com.example.Try1.busDisp.onCreate(busDisp.java:91)
12-20 19:55:57.701: E/WindowManager(982):   at android.app.Activity.performCreate(Activity.java:4465)
12-20 19:55:57.701: E/WindowManager(982):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
12-20 19:55:57.701: E/WindowManager(982):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
12-20 19:55:57.701: E/WindowManager(982):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
12-20 19:55:57.701: E/WindowManager(982):   at android.app.ActivityThread.access$600(ActivityThread.java:127)
12-20 19:55:57.701: E/WindowManager(982):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
12-20 19:55:57.701: E/WindowManager(982):   at android.os.Handler.dispatchMessage(Handler.java:99)
12-20 19:55:57.701: E/WindowManager(982):   at android.os.Looper.loop(Looper.java:137)
12-20 19:55:57.701: E/WindowManager(982):   at android.app.ActivityThread.main(ActivityThread.java:4441)
12-20 19:55:57.701: E/WindowManager(982):   at java.lang.reflect.Method.invokeNative(Native Method)
12-20 19:55:57.701: E/WindowManager(982):   at java.lang.reflect.Method.invoke(Method.java:511)
12-20 19:55:57.701: E/WindowManager(982):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
12-20 19:55:57.701: E/WindowManager(982):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
12-20 19:55:57.701: E/WindowManager(982):   at dalvik.system.NativeStart.main(Native Method)

我在第 249 行受到逼迫。

4

2 回答 2

0

你不应该 this.dialog.dismiss();在你的 doinbackground 进程中使用。UI 更新在异步任务 Doinbackground 进程中不起作用。所有 UI 更新仅适用于 Post Execute 和 PreExecute ..

希望这会帮助你。

于 2012-12-20T15:07:24.910 回答
0

您没有在片段中包含行号onPreExecute()onPreExecute()onCreate()崩溃AsyncTask发生仍然可以轻松访问它。

在对话框上设置完所有其他内容后,您还应该调用show()last ,这样您就可以避免在它可见时更改它的状态。

于 2012-12-20T15:00:17.743 回答