2012-10-18 43 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,您的應用程序將崩潰。