应用程序在屏幕旋转时崩溃

时间:2014-03-03 11:47:54

标签: android screen-orientation

我正在线程中实现进度对话框。它在横向或纵向模式下工作正常。 但是当设备旋转时,应用程序崩溃并给出以下异常:

我的androidMainfest.xml

 <activity
        android:name="and.androidrest.Myactivity"
        android:label="@string/app_name"
        android:launchMode="standard" >

我的活动代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sax_parser);
    callDialog(); 
}
public void callDialog(){
    progressDialog = new ProgressDialog(this);
    progressDialog.setTitle("Please wait..");
    progressDialog.setCancelable(false);
    progressDialog.setMessage("Loading...");
    progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    progressDialog.show();


    new Thread(new Runnable(){
        public void run(){
            try{
                Thread.sleep(2000);           
            } 
            catch (Exception e){
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            if(progressDialog != null){ 
                try{
                    progressDialog.dismiss();
                    progressDialog=null;
                }
                catch(Exception e) {
                    e.printStackTrace();
                }          
            }            
        }
    }).start();      
}

这是我的LogCat:

03-04 09:54:53.825: E/WindowManager(1309): Activity com.example.emailsending.SaxParserActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@405217f8 that was originally added here
03-04 09:54:53.825: E/WindowManager(1309): android.view.WindowLeaked: Activity com.example.emailsending.SaxParserActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@405217f8 that was originally added here
03-04 09:54:53.825: E/WindowManager(1309):  at android.view.ViewRoot.<init>(ViewRoot.java:258)
03-04 09:54:53.825: E/WindowManager(1309):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
03-04 09:54:53.825: E/WindowManager(1309):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
03-04 09:54:53.825: E/WindowManager(1309):  at android.view.Window$LocalWindowManager.addView(Window.java:424)
03-04 09:54:53.825: E/WindowManager(1309):  at android.app.Dialog.show(Dialog.java:241)
03-04 09:54:53.825: E/WindowManager(1309):  at com.example.emailsending.SaxParserActivity.callDialog(SaxParserActivity.java:157)
03-04 09:54:53.825: E/WindowManager(1309):  at com.example.emailsending.SaxParserActivity.onCreate(SaxParserActivity.java:52)
03-04 09:54:53.825: E/WindowManager(1309):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-04 09:54:53.825: E/WindowManager(1309):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
03-04 09:54:53.825: E/WindowManager(1309):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
03-04 09:54:53.825: E/WindowManager(1309):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-04 09:54:53.825: E/WindowManager(1309):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
03-04 09:54:53.825: E/WindowManager(1309):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-04 09:54:53.825: E/WindowManager(1309):  at android.os.Looper.loop(Looper.java:123)
03-04 09:54:53.825: E/WindowManager(1309):  at android.app.ActivityThread.main(ActivityThread.java:3683)
03-04 09:54:53.825: E/WindowManager(1309):  at java.lang.reflect.Method.invokeNative(Native Method)
03-04 09:54:53.825: E/WindowManager(1309):  at java.lang.reflect.Method.invoke(Method.java:507)
03-04 09:54:53.825: E/WindowManager(1309):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-04 09:54:53.825: E/WindowManager(1309):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-04 09:54:53.825: E/WindowManager(1309):  at dalvik.system.NativeStart.main(Native Method)
03-04 09:54:53.906: D/dalvikvm(1309): GC_EXTERNAL_ALLOC freed 107K, 52% free 2616K/5379K, external 910K/1038K, paused 66ms
03-04 09:54:53.995: D/AndroidRuntime(1309): Shutting down VM
03-04 09:54:53.995: W/dalvikvm(1309): threadid=1: thread exiting with uncaught exception (group=0x40015560)
03-04 09:54:54.005: E/AndroidRuntime(1309): FATAL EXCEPTION: main
03-04 09:54:54.005: E/AndroidRuntime(1309): java.lang.IllegalArgumentException: View not attached to window manager
03-04 09:54:54.005: E/AndroidRuntime(1309):     at android.view.WindowManagerImpl.findViewLocked(WindowManagerImpl.java:355)
03-04 09:54:54.005: E/AndroidRuntime(1309):     at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:200)
03-04 09:54:54.005: E/AndroidRuntime(1309):     at android.view.Window$LocalWindowManager.removeView(Window.java:432)
03-04 09:54:54.005: E/AndroidRuntime(1309):     at android.app.Dialog.dismissDialog(Dialog.java:278)
03-04 09:54:54.005: E/AndroidRuntime(1309):     at android.app.Dialog.access$000(Dialog.java:71)
03-04 09:54:54.005: E/AndroidRuntime(1309):     at android.app.Dialog$1.run(Dialog.java:111)
03-04 09:54:54.005: E/AndroidRuntime(1309):     at android.os.Handler.handleCallback(Handler.java:587)
03-04 09:54:54.005: E/AndroidRuntime(1309):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-04 09:54:54.005: E/AndroidRuntime(1309):     at android.os.Looper.loop(Looper.java:123)
03-04 09:54:54.005: E/AndroidRuntime(1309):     at android.app.ActivityThread.main(ActivityThread.java:3683)
03-04 09:54:54.005: E/AndroidRuntime(1309):     at java.lang.reflect.Method.invokeNative(Native Method)
03-04 09:54:54.005: E/AndroidRuntime(1309):     at java.lang.reflect.Method.invoke(Method.java:507)
03-04 09:54:54.005: E/AndroidRuntime(1309):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-04 09:54:54.005: E/AndroidRuntime(1309):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-04 09:54:54.005: E/AndroidRuntime(1309):     at dalvik.system.NativeStart.main(Native Method)

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

您在活动中使用进度dailog。所以当你旋转手机然后它会抛出泄漏窗口异常..因为你需要在旋转时间消除进度......

使用它可以解决您的问题

@Override
public void onPause() 
{
    super.onPause();

    if(progressDialog != null)
    {
        progressDialog.dismiss();
    }

}