我想对横向和纵向使用不同的布局。我已将 xml 文件分别放入 layout 和 layout-land 中。
对于我的父活动,我设置了 android:configChanges="keyboardHidden|orientation|screenSize" 所以我想自己处理方向更改。
我试过这个手动更改布局:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
getFragmentManager().beginTransaction().replace(R.id.content_frame, new FoodDashboardFragment()).commit();
}
它可以工作......但是当进入主屏幕时应用程序崩溃......所以..我猜我错过了一些东西。还有另一种(简单)方法吗?
这是日志:
07-23 17:58:46.717: E/AndroidRuntime(862): FATAL EXCEPTION: main
07-23 17:58:46.717: E/AndroidRuntime(862): java.lang.IllegalStateException: Fragment FoodDashboardFragment{41a06e30} is not currently in the FragmentManager
07-23 17:58:46.717: E/AndroidRuntime(862): at android.support.v4.app.FragmentManagerImpl.putFragment(FragmentManager.java:546)
07-23 17:58:46.717: E/AndroidRuntime(862): at com.android.restauranthanovra.MainActivity.onSaveInstanceState(MainActivity.java:167)
07-23 17:58:46.717: E/AndroidRuntime(862): at android.app.Activity.performSaveInstanceState(Activity.java:1137)
07-23 17:58:46.717: E/AndroidRuntime(862): at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1215)
07-23 17:58:46.717: E/AndroidRuntime(862): at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:2951)
07-23 17:58:46.717: E/AndroidRuntime(862): at android.app.ActivityThread.handleStopActivity(ActivityThread.java:3010)
07-23 17:58:46.717: E/AndroidRuntime(862): at android.app.ActivityThread.access$900(ActivityThread.java:130)
07-23 17:58:46.717: E/AndroidRuntime(862): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1222)
07-23 17:58:46.717: E/AndroidRuntime(862): at android.os.Handler.dispatchMessage(Handler.java:99)
07-23 17:58:46.717: E/AndroidRuntime(862): at android.os.Looper.loop(Looper.java:137)
07-23 17:58:46.717: E/AndroidRuntime(862): at android.app.ActivityThread.main(ActivityThread.java:4745)
07-23 17:58:46.717: E/AndroidRuntime(862): at java.lang.reflect.Method.invokeNative(Native Method)
07-23 17:58:46.717: E/AndroidRuntime(862): at java.lang.reflect.Method.invoke(Method.java:511)
07-23 17:58:46.717: E/AndroidRuntime(862): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
07-23 17:58:46.717: E/AndroidRuntime(862): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-23 17:58:46.717: E/AndroidRuntime(862): at dalvik.system.NativeStart.main(Native Method)