安卓无法找到固定方向活动的布局资源

3

我有一个应用程序,使用以下属性固定所有元素的横向方向:

android:screenOrientation="sensorLandscape"
android:configChanges="orientation|screenLayout"

所有的活动布局文件都在layout-land/文件夹中,而不是在layout/中。

这一切都运作良好,在Galaxy Tab上运行Android 3.1时,如果在纵向方向启动应用程序,就会出现以下异常:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp/com.myapp.MainMenu}: android.content.res.Resources$NotFoundException: Resource ID #0x7f030006
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1751)
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1767)
     at android.app.ActivityThread.access$1500(ActivityThread.java:122)
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1005)
     at android.os.Handler.dispatchMessage(Handler.java:99)
     at android.os.Looper.loop(Looper.java:132)
     at android.app.ActivityThread.main(ActivityThread.java:4028)
     at java.lang.reflect.Method.invokeNative(Native Method)
     at java.lang.reflect.Method.invoke(Method.java:491)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
     at dalvik.system.NativeStart.main(Native Method)
 Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f030006
     at android.content.res.Resources.getValue(Resources.java:1014)
     at android.content.res.Resources.loadXmlResourceParser(Resources.java:2039)
     at android.content.res.Resources.getLayout(Resources.java:853)
     at android.view.LayoutInflater.inflate(LayoutInflater.java:389)
     at android.view.LayoutInflater.inflate(LayoutInflater.java:347)
     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:242)
     at android.app.Activity.setContentView(Activity.java:1780)
     at com.myapp.MainMenu.onCreate(MainMenu.java:77)
     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1715)
     ... 11 more

这个问题出现在初始 Activity 的 onCreate() 方法中的 setContentView() 调用中:
setContentView(R.layout.mainmenu);
我通过将所有布局文件从 layout-land/ 复制到 layout/ 中来解决了这个问题,但这似乎不是一个很好的解决方案,因为现在我有两套需要保持同步的布局文件。更加奇怪的是,在异常发生之前记录 getRequestedOrientation() 的值显示它返回 6(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE)。因此,我不知道为什么 setContentView() 没有查找 layout-land/。
在 2.3 手机上竖屏启动应用程序时,我没有看到这个问题,但在手机屏幕关闭时启动应用程序会产生基本相同的异常,也可以通过将布局文件复制到 layout/ 中来解决。
有人见过这个错误吗?或许有更好的解决方法?

好的,我刚刚在这里查看了(http://developer.android.com/guide/topics/resources/providing-resources.html#Compatibility),看起来最佳实践是如果您只支持一种方向,则将所有布局资源放在**land/**中。 - robinj
这里有一个类似的问题链接;问题确实是当布局在设备配置解析为纵向时加载时,横向特定资源被过滤掉,在当前情况下将不会有任何匹配给定ID的资源。问题仍然存在,即为什么系统在纵向配置下执行上述代码。 - desseim
1个回答

0

我在三星Galaxy S3上遇到了类似的错误。

我只有一个方向:竖屏。所以我把所有的layout-port文件移动到layout文件夹中,并删除layout-port文件夹来解决这个问题。 另外,我也可以通过将文件放在layout-landlayout-port中来解决这个问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接