xiaoxiaoaishang 2012-09-11 06:00 采纳率: 100%
浏览 4457
已结题

当进度对话框和后台线程活动的时候如何处理屏幕的方向的变化?

我的程序是在一个后台的线程里进行网络活动。在开始之前,它弹出一个进度对话框。这个对话框是处理器上被驳回的。除了当对话框出现的时候屏幕的方向发生变换(后台线程也跟随着发生变化),其他的都是正常的。在时,应用或者崩溃,有时候死锁,或者是当所有的线程都被杀死的时候进入到应用,否则一点都不起作用这样一个怪异的阶段。
我能怎样处理屏幕方向发生变化这个问题?
下面的示例代码大概就是我的真正的程序:

public class MyAct extends Activity implements Runnable {
    public ProgressDialog mProgress;

    // UI has a button that when pressed calls send

    public void send() {
         mProgress = ProgressDialog.show(this, "Please wait", 
                      "Please wait", 
                      true, true);
        Thread thread = new Thread(this);
        thread.start();
    }

    public void run() {
        Thread.sleep(10000);
        Message msg = new Message();
        mHandler.sendMessage(msg);
    }

    private final Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            mProgress.dismiss();
        }
    };
}

栈:

E/WindowManager(  244): Activity MyAct has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@433b7150 that was originally added here
E/WindowManager(  244): android.view.WindowLeaked: Activity MyAct has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@433b7150 that was originally added here
E/WindowManager(  244):     at android.view.ViewRoot.<init>(ViewRoot.java:178)
E/WindowManager(  244):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:147)
E/WindowManager(  244):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:90)
E/WindowManager(  244):     at android.view.Window$LocalWindowManager.addView(Window.java:393)
E/WindowManager(  244):     at android.app.Dialog.show(Dialog.java:212)
E/WindowManager(  244):     at android.app.ProgressDialog.show(ProgressDialog.java:103)
E/WindowManager(  244):     at android.app.ProgressDialog.show(ProgressDialog.java:91)
E/WindowManager(  244):     at MyAct.send(MyAct.java:294)
E/WindowManager(  244):     at MyAct$4.onClick(MyAct.java:174)
E/WindowManager(  244):     at android.view.View.performClick(View.java:2129)
E/WindowManager(  244):     at android.view.View.onTouchEvent(View.java:3543)
E/WindowManager(  244):     at android.widget.TextView.onTouchEvent(TextView.java:4664)
E/WindowManager(  244):     at android.view.View.dispatchTouchEvent(View.java:3198)
E/WindowManager(  244):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:857)
E/WindowManager(  244):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:857)
E/WindowManager(  244):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:857)
E/WindowManager(  244):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:857)
E/WindowManager(  244):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:857)
E/WindowManager(  244):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1593)
E/WindowManager(  244):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1089)
E/WindowManager(  244):     at android.app.Activity.dispatchTouchEvent(Activity.java:1871)
E/WindowManager(  244):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1577)
E/WindowManager(  244):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1140)
E/WindowManager(  244):     at android.os.Handler.dispatchMessage(Handler.java:88)
E/WindowManager(  244):     at android.os.Looper.loop(Looper.java:123)
E/WindowManager(  244):     at android.app.ActivityThread.main(ActivityThread.java:3739)
E/WindowManager(  244):     at java.lang.reflect.Method.invokeNative(Native Method)
E/WindowManager(  244):     at java.lang.reflect.Method.invoke(Method.java:515)
E/WindowManager(  244):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
E/WindowManager(  244):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:497)
E/WindowManager(  244):     at dalvik.system.NativeStart.main(Native Method)

和:

W/dalvikvm(  244): threadid=3: thread exiting with uncaught exception (group=0x4000fe68)
E/AndroidRuntime(  244): Uncaught handler: thread main exiting due to uncaught exception
E/AndroidRuntime(  244): java.lang.IllegalArgumentException: View not attached to window manager
E/AndroidRuntime(  244):    at android.view.WindowManagerImpl.findViewLocked(WindowManagerImpl.java:331)
E/AndroidRuntime(  244):    at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:200)
E/AndroidRuntime(  244):    at android.view.Window$LocalWindowManager.removeView(Window.java:401)
E/AndroidRuntime(  244):    at android.app.Dialog.dismissDialog(Dialog.java:249)
E/AndroidRuntime(  244):    at android.app.Dialog.access$000(Dialog.java:59)
E/AndroidRuntime(  244):    at android.app.Dialog$1.run(Dialog.java:93)
E/AndroidRuntime(  244):    at android.app.Dialog.dismiss(Dialog.java:233)
E/AndroidRuntime(  244):    at MyAct$1.handleMessage(MyAct.java:321)
E/AndroidRuntime(  244):    at android.os.Handler.dispatchMessage(Handler.java:88)
E/AndroidRuntime(  244):    at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(  244):    at android.app.ActivityThread.main(ActivityThread.java:3739)
E/AndroidRuntime(  244):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(  244):    at java.lang.reflect.Method.invoke(Method.java:515)
E/AndroidRuntime(  244):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
E/AndroidRuntime(  244):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:497)
E/AndroidRuntime(  244):    at dalvik.system.NativeStart.main(Native Method)
I/Process (   46): Sending signal. PID: 244 SIG: 3
I/dalvikvm(  244): threadid=7: reacting to signal 3
I/dalvikvm(  244): Wrote stack trace to '/data/anr/traces.txt'
I/Process (  244): Sending signal. PID: 244 SIG: 9
I/ActivityManager(   46): Process MyAct (pid 244) has died.

我试图在onSaveInstanceState上取消对话框的进度条,但是这只能防止立即崩溃。后台线程仍然在运行,而且这个用户界面有一部分是处于拉伸状态的。需要杀掉整个应用程序才会重新开始工作。

  • 写回答

5条回答 默认 最新

  • Sueyexin 2012-09-11 06:20
    关注

    当你转换方向的时候,android将会创建一个新的视图。你的程序可能会崩溃,因为你的后台线程是试图在一个老的视图上改变这个场景。(它可能也会有问题,因为你的后台的线程不是在UI的线程上)
    我建议当方向改变的时候改变处理器并且进行更新。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。