开发者

Android: Handling orientation changes myself - RelativeLayout is getting messed up

In an Android app, I've got an activity that is somewhat complex and may have threads running at certain times which, when finished, will update the UI. As such, having the Activity destroy and create again (due to orientation change) in the middle of one of these threads could lead to the user having to re-try that action, which is bad.

As such, I've decided to handle orientation changes myself by having android:configChanges="orientation|keyboardHidden" in the manifest, and can then override the Activity.onConfigurationChanged(Configuration newConfig) method.

The root layout for this activity is a RelativeLayout. Currently, I don't actually do anything in onConfigurationChanged() other than call the super().

After an orientation change, the layout is muddled - some elements which are android:layout_above= are either at the bottom of the screen, or floating way above what they should be floating above.

Does anyone have any ideas as to how to fix this?

I'm aware that if I allowed the activity to restart upon an orientation change, it would then use a layout from res/layout-land/, but restarting the activity really seems to be the wrong way to go here.

Edit:

I've tried to setContentView(R.layout.displaymap); in the onConfigurationChanged() method and get the following error:

I/WindowManager(  571): onOrientationChanged, rotation changed to 1
D/StatusBar(  571): updateResources
E/AndroidRuntime( 3813): android.view.InflateException: Binary XML file line #17: Error inflating class java.lang.reflect.Constructor
E/AndroidRuntime( 3813):        at android.view.LayoutInflater.createView(LayoutInflater.java:512)
E/AndroidRuntime( 3813):        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:564)
E/AndroidRuntime( 3813):        at android.view.LayoutInflater.rInflate(LayoutInflater.java:617)
E/AndroidRuntime( 3813):        at android.view.LayoutInflater.rInflate(LayoutInflater.java:620)
E/AndroidRuntime( 3813):        at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
E/AndroidRuntime( 3813):        at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
E/AndroidRuntime( 3813):        at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
E/AndroidRuntime( 3813):        at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:309)
E/AndroidRuntime( 3813):        at android.app.Activity.setContentView(Activity.java:1626)
E/AndroidRuntime( 3813):        at com.apps.virtualtravel.DisplayMap.onConfigurationChanged(DisplayMap.java:1065)
E/AndroidRuntime( 3813):        at android.app.ActivityThread.performConfigurationChanged(ActivityThread.java:3383)
E/AndroidRuntime( 3813):        at android.app.ActivityThread.handleConfigurationChanged(ActivityThread.java:3449)
E/AndroidRuntime( 3813):        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1762)
E/AndroidRuntime( 3813):        at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 3813):        at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime( 3813):        at android.ap开发者_StackOverflow中文版p.ActivityThread.main(ActivityThread.java:3948)
E/AndroidRuntime( 3813):        at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 3813):        at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime( 3813):        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
E/AndroidRuntime( 3813):        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
E/AndroidRuntime( 3813):        at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 3813): Caused by: java.lang.reflect.InvocationTargetException
E/AndroidRuntime( 3813):        at com.google.android.maps.MapView.<init>(MapView.java:237)
E/AndroidRuntime( 3813):        at java.lang.reflect.Constructor.constructNative(Native Method)
E/AndroidRuntime( 3813):        at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
E/AndroidRuntime( 3813):        at android.view.LayoutInflater.createView(LayoutInflater.java:499)
E/AndroidRuntime( 3813):        ... 20 more
E/AndroidRuntime( 3813): Caused by: java.lang.IllegalStateException: You are only allowed to have a single MapView in a MapActivity
E/AndroidRuntime( 3813):        at com.google.android.maps.MapActivity.setupMapView(MapActivity.java:180)
E/AndroidRuntime( 3813):        at com.google.android.maps.MapView.<init>(MapView.java:279)
E/AndroidRuntime( 3813):        at com.google.android.maps.MapView.<init>(MapView.java:254)
E/AndroidRuntime( 3813):        ... 24 more


You have to create layout for landscape mode if you want to set for landscape and put the layout in layout-land folder with same name which is for portrait and than setContentview(R.layout.name) in onConfigChanged method. this will give the desired UI.other wise you will face issue in RelativeLayout.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜