方向改变时相机出现问题
我正在开发一个应用程序,当设备方向从横向变为纵向或反之亦然时,该应用程序会调用不同的活动。横向模式的活动是增强现实活动,因此我使用相机向用户展示周围环境。但是,当我尝试返回纵向时,应用程序崩溃并显示以下错误:
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 清单(活动“EmergciesList”是纵向模式的活动,“MapaMovilAR”是横向模式的活动):
<?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();
}
}
有什么可能导致崩溃吗?
I'm doing an application that calls a different activity when the device orientation changes from landscape to portrait or viceversa. The activity for the landscape mode is an augmented reality one, so I use the camera to show the surroundings to the user. However, when I try to go back to portrait, the app crashes and displays this error:
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)
The change from portrait to landscape works normally.
This is my android manifest (the activity "EmergenciesList" is the one for portrait mode, and "MapaMovilAR" for 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>
And finally this is the code for handling the orientation change:
@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();
}
}
Is there anything that could be causing the crash?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 :
Use :
Camera.setDisplayOrientation 只能使用 0, 90, 180, 270
Camera.setDisplayOrientation only can use 0, 90, 180, 270
logcat 错误读数中通常有一个“由”行引起,您能否查看它是否存在并包含它(如果存在)?它将有助于提供更多见解。
当方向发生变化时,活动将被终止。我最初的猜测是,您设置的某些值在返回纵向模式时不再有效。您可以使用savedInstanceState 包来传递并保留这些更改。
there's usually a "caused by" line in the logcat error readout, can you see if it's there and include it if it is? It will help provide more insight.
When an orientation change happens the activity is killed. My initial guess is that you are setting some value that is not longer valid when you return to portrait mode. You can used the savedInstanceState bundle to pass along and persist these changes.