异步任务 - 强制关闭

时间:2012-12-20 14:30:27

标签: android android-asynctask forceclose

此调用在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

之后有PostExecute12-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)

这是日志:

{{1}}

我在第249行接近一支部队。

2 个答案:

答案 0 :(得分:0)

你没有在你的剪辑中包含行号,但崩溃发生在onPreExecute()看起来像是窗口令牌的问题,也许你应该在onPreExecute()从头开始创建对话框或创建它在onCreate()中并使其成为类成员,这样您的AsyncTask仍然可以轻松访问它。

一旦你在对话框中设置了其他所有内容,你也应该最后调用show(),这样你就可以避免在可见的情况下改变它的状态。

答案 1 :(得分:0)

您不应在doinbackground流程中使用this.dialog.dismiss();。 UI更新在Async Task Doinbackground进程中不起作用。 所有UI更新仅适用于Post Execute和PreExecute ..

希望这会对你有所帮助。