문제

My Activity only supports landscape mode by specifying

android:screenOrientation="sensorLandscape" 

in manifest.

Now if my Activity is in foreground and some other app, which supports both orientation, gets launched and user changes orientation to portrait and hits back then my Activity gets crashed throwing:

android.content.res.Resources$NotFoundException

I guess it looks for layout resources for portrait mode briefly and layouts are only in layout-land so it get crashed.

E.g. I launch my app, lock the screen, rotate device to portrait mode, unlock device and I can see crash and ANR.

Any idea how to resolve this issue?

도움이 되었습니까?

해결책

Copy your layouts to the portrait directories (res/layout), in this brief moment when the system tries to render your layout then it won't crash from a resource not found exception

다른 팁

Just you need to same copy of xml into res/layout, I think it should work, because android search all app resource according to current configuration of device(Like orientation,screensize and etc) in respective folder ( identifiers folder) if it not find-able in that folder then android will extend their search in default folder `like res/layout, res/drawable,res/string etc.

So the reasons of crashing your app Your layout was not present in default folder, so whenever configuration was other then landscape it will definitly crash.

Identifiers folder example.

res/ layout/ main.xml ---> for portrait screen
layout-land/ main.xml ---> for landscap screen

res/ layout/ # default (portrait)
main.xml layout-land/ # landscape
main.xml layout-large/ # large (portrait)
main.xml layout-large-land/ # large landscape
main.xml

res/ drawable-xhdpi/
awesomeimage.png
drawable-hdpi/
awesomeimage.png
drawable-mdpi/
awesomeimage.png
drawable-ldpi/
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top