2012-10-18 50 views
8

我正在开发一个Android应用程序,应该只能在肖像模式下运行,由于布局不适合手机的横向屏幕。 但是,在平板电脑和上网本上,我希望应用只能在横向模式下运行。setRequestedOrientation之前显示ProgressDialog导致崩溃

我现在试着检查应用程序是否在平板设备上运行,并通过setRequestedOrientation设置相应的请求方向。

问题是,当设备未按照我要求的方向保持设备时,应用程序崩溃,因为我在调用setRequestedOrientation之后不久显示了一个progressDialog,它似乎泄漏了一个窗口。

logcat的说:

10-18 21:15:30.698: E/WindowManager(653): Activity has leaked window [email protected] that was originally added here 
10-18 21:15:30.698: E/WindowManager(653): android.view.WindowLeaked: Activity has leaked window [email protected] that was originally added here 
10-18 21:15:30.698: E/WindowManager(653): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:279) 
10-18 21:15:30.698: E/WindowManager(653): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215) 
10-18 21:15:30.698: E/WindowManager(653): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:140) 
10-18 21:15:30.698: E/WindowManager(653): at android.view.Window$LocalWindowManager.addView(Window.java:537) 
10-18 21:15:31.888: E/WindowManager(653): Activity has leaked window [email protected] that was originally added here 
10-18 21:15:31.888: E/WindowManager(653): android.view.WindowLeaked: Activity has leaked window [email protected] that was originally added here 
10-18 21:15:31.888: E/WindowManager(653): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:279) 
10-18 21:15:31.888: E/WindowManager(653): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215) 
10-18 21:15:31.888: E/WindowManager(653): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:140) 
10-18 21:15:31.888: E/WindowManager(653): at android.view.Window$LocalWindowManager.addView(Window.java:537) 
10-18 21:15:34.168: E/AndroidRuntime(653): at android.view.WindowManagerImpl.findViewLocked(WindowManagerImpl.java:599) 
10-18 21:15:34.168: E/AndroidRuntime(653): at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:336) 
10-18 21:15:34.168: E/AndroidRuntime(653): at android.view.WindowManagerImpl$CompatModeWrapper.removeView(WindowManagerImpl.java:151) 

我能做什么来防止这种崩溃? 任何帮助将不胜感激。

编辑: 由于我无法解决这个问题,我终于设法编辑我的布局,现在它允许在纵向和横向模式下使用。

回答

11

你有什么源代码可以显示吗?这可能有助于确定问题。

我实际上有完全相同的问题。但这只是发生在我的一些活动上。

当屏幕方向改变时,Android实际上会破坏并重新创建活动。

所以,我看起来像这样的代码。

@Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 

     setContentView(R.layout.displayscreen); 

     bottomButton = (Button) findViewById(R.id.bottomButton); 
     bottomButton.setOnClickListener(bottomButtonClick); 
     bottomButton.setTypeface(font); 
     bottomButton.setTextSize(16); 
} 

看看发生了什么是该视图没有正确地附加到窗口管理器。所以我决定创建者可能不是最好的选择。

相反,我把它添加到我的简历,它的工作原理。像这样:

@Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.displayscreen); 

     bottomButton = (Button) findViewById(R.id.bottomButton); 
     bottomButton.setOnClickListener(bottomButtonClick); 
     bottomButton.setTypeface(font); 
     bottomButton.setTextSize(16); 
} 

@Override 
protected void onResume() { 
    super.onResume(); 
    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
} 

不幸的是,这也导致活动仍然被破坏和重新创建。两次调用onCreate和onResume ...不正确?

所以要解决这个问题。你必须将它添加到你的活动的Android清单中。

android:configChanges="keyboardHidden|orientation" 

为例:

<activity 
    android:name="com.Test.Info.DisplayInfo" 
    android:configChanges="keyboardHidden|orientation" 
    android:label="@string/info"> 
</activity> 

此代码防止了破坏/重新循环。

希望这有助于!

干杯

+1

感谢您编写这么长的答案,但不幸的是它并没有解决我的问题。 –

+0

没问题。对不起,它并没有帮助你。下次如果您添加一些源代码,它将有助于追踪您的具体问题。我有类似的问题,这对我的具体问题有效。很高兴你的工作虽然。 – Dave

+1

谢谢,你的回答救了我戴夫!实际上,你只需要在清单中设置'android:configChanges ='orientation''并在'onCreate()'的开头调用'setRequestedOrientation()';) –

9

如果使用

setRequestedOrientation(SCREEN_ORIENTATION_NOSENSOR); 

暂时锁住,并

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR); 

解锁屏幕旋转,也许你遇到了以下problem

如果您使用此方法锁定屏幕方向并且设备未处于其默认方向,它将切换到默认方向,从而销毁和创建您的活动。由于您试图更新一些destoryed progressDialog,您的应用程序将崩溃。