http://stackoverflow.com/questions/9415574/fragment-onconfigurationchanged-duplicate
在学习Android Application Development Cookbook 93 Recipes For Building Winning Apps的第Recipe 3.10时候,在真机上如果想转屏显示,总会出现duplicate id问题导致应用挂了。
Caused by: java.lang.IllegalArgumentException: Binary XML file line #6: Duplicate id 0x7f090008, tag null, or parent id 0xffffffff with another fragment for com.febro.myfragmenttest.ListFragment
难道说写成android_landscape_main.xml和android_main.xml在
onConfigurationChanged的时候没有调用
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
setContentView(R.layout.activity_main);
} else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
setContentView(R.layout.activity_landscape_main);
}
}
在反复调试之后,发现不是这里的问题。
最后在stackoverflow上找到了答案。
You should let android do all that configuration switching for you! Don't do anything in "onConfigurationChanged", remove all configuration changes from your manifest even!
Put your portrait layout in layout/ folder
and put your landscape layout in layout-land/ folder
Make sure they are both named: main.xml (or anything, as long as it is the same)
put your portrait layout in /layout/ and your landscape layout in /layout-land/. They can be completely different, just need the same name. To save and restore data, save your data in onSaveInstanceState and restore it in in onCreate when the Bundle != null