在旋转Android应用程序崩溃

时间:2012-05-08 11:23:45

标签: android android-layout

我有一个在纵向和横向中使用两种布局的应用程序。要在初始加载时从Web加载内容,我使用AsyncTask和进度条。

我查了this。由于我有两个布局,这不是解决方案。

以下是例外情况。

> 05-08 16:44:43.483: E/WindowManager(20138): Activity com.news.one has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@482b0130 that was originally added here
05-08 16:44:43.483: E/WindowManager(20138): android.view.WindowLeaked: Activity com.news.one has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@482b0130 that was originally added here
05-08 16:44:43.483: E/WindowManager(20138):     at android.view.ViewRoot.<init>(ViewRoot.java:247)
05-08 16:44:43.483: E/WindowManager(20138):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:181)
05-08 16:44:43.483: E/WindowManager(20138):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:124)
05-08 16:44:43.483: E/WindowManager(20138):     at android.view.Window$LocalWindowManager.addView(Window.java:424)
05-08 16:44:43.483: E/WindowManager(20138):     at android.app.Dialog.show(Dialog.java:241)
05-08 16:44:43.483: E/WindowManager(20138):     at com.news.one$DownloadNews.onPreExecute(AdaDeranaTabActivity.java:365)
05-08 16:44:43.483: E/WindowManager(20138):     at android.os.AsyncTask.execute(AsyncTask.java:391)
05-08 16:44:43.483: E/WindowManager(20138):     at com.news.one.onCreate(AdaDeranaTabActivity.java:201)
05-08 16:44:43.483: E/WindowManager(20138):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-08 16:44:43.483: E/WindowManager(20138):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
05-08 16:44:43.483: E/WindowManager(20138):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
05-08 16:44:43.483: E/WindowManager(20138):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
05-08 16:44:43.483: E/WindowManager(20138):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
05-08 16:44:43.483: E/WindowManager(20138):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-08 16:44:43.483: E/WindowManager(20138):     at android.os.Looper.loop(Looper.java:123)
05-08 16:44:43.483: E/WindowManager(20138):     at android.app.ActivityThread.main(ActivityThread.java:4627)
05-08 16:44:43.483: E/WindowManager(20138):     at java.lang.reflect.Method.invokeNative(Native Method)
05-08 16:44:43.483: E/WindowManager(20138):     at java.lang.reflect.Method.invoke(Method.java:521)
05-08 16:44:43.483: E/WindowManager(20138):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
05-08 16:44:43.483: E/WindowManager(20138):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
05-08 16:44:43.483: E/WindowManager(20138):     at dalvik.system.NativeStart.main(Native Method)
05-08 16:44:54.065: E/AndroidRuntime(20138): FATAL EXCEPTION: main
05-08 16:44:54.065: E/AndroidRuntime(20138): java.lang.IllegalArgumentException: View not attached to window manager
05-08 16:44:54.065: E/AndroidRuntime(20138):    at android.view.WindowManagerImpl.findViewLocked(WindowManagerImpl.java:388)
05-08 16:44:54.065: E/AndroidRuntime(20138):    at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:233)
05-08 16:44:54.065: E/AndroidRuntime(20138):    at android.view.Window$LocalWindowManager.removeView(Window.java:432)
05-08 16:44:54.065: E/AndroidRuntime(20138):    at android.app.Dialog.dismissDialog(Dialog.java:278)
05-08 16:44:54.065: E/AndroidRuntime(20138):    at android.app.Dialog.access$000(Dialog.java:71)
05-08 16:44:54.065: E/AndroidRuntime(20138):    at android.app.Dialog$1.run(Dialog.java:111)
05-08 16:44:54.065: E/AndroidRuntime(20138):    at android.app.Dialog.dismiss(Dialog.java:268)
05-08 16:44:54.065: E/AndroidRuntime(20138):    at com.news.one$DownloadNews.onPostExecute(AdaDeranaTabActivity.java:376)
05-08 16:44:54.065: E/AndroidRuntime(20138):    at com.news.one$DownloadNews.onPostExecute(AdaDeranaTabActivity.java:1)
05-08 16:44:54.065: E/AndroidRuntime(20138):    at android.os.AsyncTask.finish(AsyncTask.java:417)
05-08 16:44:54.065: E/AndroidRuntime(20138):    at android.os.AsyncTask.access$300(AsyncTask.java:127)
05-08 16:44:54.065: E/AndroidRuntime(20138):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
05-08 16:44:54.065: E/AndroidRuntime(20138):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-08 16:44:54.065: E/AndroidRuntime(20138):    at android.os.Looper.loop(Looper.java:123)
05-08 16:44:54.065: E/AndroidRuntime(20138):    at android.app.ActivityThread.main(ActivityThread.java:4627)
05-08 16:44:54.065: E/AndroidRuntime(20138):    at java.lang.reflect.Method.invokeNative(Native Method)
05-08 16:44:54.065: E/AndroidRuntime(20138):    at java.lang.reflect.Method.invoke(Method.java:521)
05-08 16:44:54.065: E/AndroidRuntime(20138):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
05-08 16:44:54.065: E/AndroidRuntime(20138):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
05-08 16:44:54.065: E/AndroidRuntime(20138):    at dalvik.system.NativeStart.main(Native Method)

是否有一种锁定旋转变化的方法,直到应用程序自行加载?

或者其他想法?

提前致谢。

4 个答案:

答案 0 :(得分:1)

切换方向时,Android会创建一个新视图。您可能正在崩溃,因为您的后台线程正在尝试更改旧的状态。 (它可能也有问题,因为你的后台线程不在UI线程上)。 这些是解决方案:

How to handle screen orientation change when progress dialog and background thread active?

Activity restart on rotation Android

答案 1 :(得分:0)

我在progressDialog.dismiss()方法中调用onDestroy()

在方向更改期间,活动将重新开始。所以在重启之前我们必须销毁当前的progressDialog。这样就可以了。 :d

答案 2 :(得分:-1)

您正在获取WindowLeak Exception,当任何dialog / progressBar使用由于方向更改而丢失的上下文和上下文引用的引用时,会发生此异常。

因此,请在清单文件中为所有活动标记添加以下代码。

android:configChanges="orientation|keyboard"

示例:

<activity
            android:name=".InviteActivity" android:configChanges="orientation|keyboard"/>

并在活动中使用以下代码

 @Override
    public void onConfigurationChanged(Configuration newConfig)
    {
        setContentView(R.layout.YOUR_LAYOUT);
        super.onConfigurationChanged(newConfig);
    }

答案 3 :(得分:-1)

我遇到了同样的问题并解决了这个问题,我在开始同步之前锁定了方向并在同步后将其设置回来:

// before
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);

// after synchronisation: 
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);