Facing a problem when switching between portrait/landscape orientation
When I try to switch to landscape orientation of the screen I get an error. The XML layout for landscape orientation work just fine, because I tested it by forcing the application to appear in landscape orientation in AndoridManifest.xml:
android:screenOrientation="portrait"
Here is a log-trace from LogCat:
08-03 09:09:33.660: INFO/WindowManager(71): Setting rotation to 1, animFlags=1
08-03 09:09:33.690: INFO/WindowManager(71): Config changed: { scale=1.0 imsi=0/0 loc=en_US touch=3 keys=2/1/1 nav=3 orien=2 layout=17}
08-03 09:09:33.849: INFO/WindowM开发者_StackOverflow社区anager(71): onOrientationChanged, rotation changed to 1
08-03 09:09:34.010: DEBUG/StatusBar(71): updateResources
08-03 09:09:34.251: DEBUG/AndroidRuntime(479): Shutting down VM
08-03 09:09:34.251: WARN/dalvikvm(479): threadid=3: thread exiting with uncaught exception (group=0x4001aa28)
08-03 09:09:34.251: ERROR/AndroidRuntime(479): Uncaught handler: thread main exiting due to uncaught exception
08-03 09:09:34.280: ERROR/AndroidRuntime(479): android.util.AndroidRuntimeException: requestFeature() must be called before adding content
08-03 09:09:34.280: ERROR/AndroidRuntime(479): at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:285)
08-03 09:09:34.280: ERROR/AndroidRuntime(479): at android.app.Activity.requestWindowFeature(Activity.java:2561)
08-03 09:09:34.280: ERROR/AndroidRuntime(479): at com.news.reader.Home.onConfigurationChanged(Home.java:85)
08-03 09:09:34.280: ERROR/AndroidRuntime(479): at android.app.ActivityThread.performConfigurationChanged(ActivityThread.java:3607)
08-03 09:09:34.280: ERROR/AndroidRuntime(479): at android.app.ActivityThread.handleConfigurationChanged(ActivityThread.java:3673)
08-03 09:09:34.280: ERROR/AndroidRuntime(479): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1864)
08-03 09:09:34.280: ERROR/AndroidRuntime(479): at android.os.Handler.dispatchMessage(Handler.java:99)
08-03 09:09:34.280: ERROR/AndroidRuntime(479): at android.os.Looper.loop(Looper.java:123)
08-03 09:09:34.280: ERROR/AndroidRuntime(479): at android.app.ActivityThread.main(ActivityThread.java:4203)
08-03 09:09:34.280: ERROR/AndroidRuntime(479): at java.lang.reflect.Method.invokeNative(Native Method)
08-03 09:09:34.280: ERROR/AndroidRuntime(479): at java.lang.reflect.Method.invoke(Method.java:521)
08-03 09:09:34.280: ERROR/AndroidRuntime(479): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
08-03 09:09:34.280: ERROR/AndroidRuntime(479): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
08-03 09:09:34.280: ERROR/AndroidRuntime(479): at dalvik.system.NativeStart.main(Native Method)
08-03 09:09:34.299: INFO/Process(71): Sending signal. PID: 479 SIG: 3
08-03 09:09:34.299: INFO/dalvikvm(479): threadid=7: reacting to signal 3
08-03 09:09:34.410: INFO/dalvikvm(479): Wrote stack trace to '/data/anr/traces.txt'
There is an error, which is telling me that I should do something before setting the layout content. Could some one explain what I am missing or give redirection, where this is explained.
UPDATE
After I called requestWindowFeature(Configuration.ORIENTATION_LANDSCAPE);
, I still got the same error:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setContentView(R.layout.home_landscape);
createUI();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.home_portrait);
createUI();
}
You are getting above exception because, you should have to call requestWindowFeature()
before setContentView()
of any Activity.
精彩评论