4

我正在做一個應用程序,當設備方向從橫向改變爲縱向或反之時,它會調用不同的活動。橫向模式的活動是增強現實模式,因此我使用相機向用戶顯示周圍環境。然而,當我試圖回到肖像,應用程序崩潰,並顯示此錯誤:方向改變時相機的問題

08-17 16:05:42.622: ERROR/AndroidRuntime(9769): FATAL EXCEPTION: main 
08-17 16:05:42.622: ERROR/AndroidRuntime(9769): java.lang.RuntimeException: set display orientation failed 
08-17 16:05:42.622: ERROR/AndroidRuntime(9769):  at android.hardware.Camera.setDisplayOrientation(Native Method) 
08-17 16:05:42.622: ERROR/AndroidRuntime(9769):  at es.ucm.look.ar.Preview.surfaceChanged(Preview.java:102) 
08-17 16:05:42.622: ERROR/AndroidRuntime(9769):  at android.view.SurfaceView.updateWindow(SurfaceView.java:549) 
08-17 16:05:42.622: ERROR/AndroidRuntime(9769):  at android.view.SurfaceView.setFrame(SurfaceView.java:294) 
08-17 16:05:42.622: ERROR/AndroidRuntime(9769):  at android.view.View.layout(View.java:7169) 
08-17 16:05:42.622: ERROR/AndroidRuntime(9769):  at android.widget.FrameLayout.onLayout(FrameLayout.java:338) 
08-17 16:05:42.622: ERROR/AndroidRuntime(9769):  at android.view.View.layout(View.java:7175) 
08-17 16:05:42.622: ERROR/AndroidRuntime(9769):  at android.widget.FrameLayout.onLayout(FrameLayout.java:338) 
08-17 16:05:42.622: ERROR/AndroidRuntime(9769):  at android.view.View.layout(View.java:7175) 
08-17 16:05:42.622: ERROR/AndroidRuntime(9769):  at android.widget.FrameLayout.onLayout(FrameLayout.java:338) 
08-17 16:05:42.622: ERROR/AndroidRuntime(9769):  at android.view.View.layout(View.java:7175) 
08-17 16:05:42.622: ERROR/AndroidRuntime(9769):  at android.view.ViewRoot.performTraversals(ViewRoot.java:1140) 
08-17 16:05:42.622: ERROR/AndroidRuntime(9769):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1859) 
08-17 16:05:42.622: ERROR/AndroidRuntime(9769):  at android.os.Handler.dispatchMessage(Handler.java:99) 
08-17 16:05:42.622: ERROR/AndroidRuntime(9769):  at android.os.Looper.loop(Looper.java:130) 
08-17 16:05:42.622: ERROR/AndroidRuntime(9769):  at android.app.ActivityThread.main(ActivityThread.java:3683) 
08-17 16:05:42.622: ERROR/AndroidRuntime(9769):  at java.lang.reflect.Method.invokeNative(Native Method) 
08-17 16:05:42.622: ERROR/AndroidRuntime(9769):  at java.lang.reflect.Method.invoke(Method.java:507) 
08-17 16:05:42.622: ERROR/AndroidRuntime(9769):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
08-17 16:05:42.622: ERROR/AndroidRuntime(9769):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
08-17 16:05:42.622: ERROR/AndroidRuntime(9769):  at dalvik.system.NativeStart.main(Native Method) 

從縱向到橫向的變化正常工作。

這是我的Android清單(活動「EmergenciesList」是一個針對肖像模式,以及「MapaMovilAR」爲lanscape):

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.list" android:versionCode="1" android:versionName="1.0"> 
<uses-permission android:name="android.permission.CAMERA" /> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
<uses-permission android:name="android.permission.INTERNET" /> 
<application android:icon="@drawable/icon" android:label="@string/app_name" 
    android:name="cl.puc.memoria.MapaMovilApplication"> 
    <activity android:name="cl.puc.memoria.EmergenciesList" 
     android:configChanges="orientation" android:label="@string/app_name"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN"></action> 
      <category android:name="android.intent.category.LAUNCHER"> </category> 
     </intent-filter> 
    </activity> 
    <activity android:name="cl.puc.memoria.AR.MapaMovilAR" 
     android:configChanges="orientation"> 
    </activity> 
    <activity android:name="es.ucm.look.ar.LookAR"></activity> 

</application> 
<uses-sdk android:minSdkVersion="7" /> 

</manifest> 

最後,這是用於處理方向變化的代碼:

@Override 
public void onConfigurationChanged(Configuration newConfig) { 
    super.onConfigurationChanged(newConfig); 

    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { 
     Intent el = new Intent(MapaMovilAR.this, 
       cl.puc.memoria.EmergenciesList.class); 
     startActivity(el); 
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) { 
     Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show(); 
    } 
} 

有什麼可能導致崩潰?

回答

1

logcat錯誤讀取中通常會有一個「由...引起」的行,您能否看到它是否存在幷包含它?這將有助於提供更多的見解。

當方向改變發生時,活動被終止。我最初的猜測是,當您返回肖像模式時,您正在設置一些不再有效的值。您可以使用savedInstanceState包傳遞並保留這些更改。

7

用途:

mCamera.stopPreview(); 
mCamera.setDisplayOrientation(mRotation); 
mCamera.startPreview(); 
+0

我這樣做,它仍然會發生。 – aaronsnoswell

+0

我得到這個錯誤以及發出mCamera.stopPreview()。我感到困惑,直到滾動logcat消息並找到調整SurfaceView大小的參考。有些無法調整大小以符合要求的大小。我從我的表面視圖中取出Weight參數,並給它一個固定的大小,這樣就解決了錯誤。 – wufoo

2

Camera.setDisplayOrientation只能用0,90,180,270