我正在开发一个有趣的色轮应用程序。0.5 版有一个由四个片段组成的调色板,一个片段用于色轮上的每个可能的颜色目标。然后根据色轮的模式隐藏或显示片段的 UI 元素。“单色”模式只需要其中一个片段,而“Accented Anaogic”则需要全部四个。当片段作为onCreate()
方法的一部分被构建和显示时,这项工作会被发现,此后再也不会被触及。
为了下一个版本改进布局,我设计了四个“包含”XML 布局文件,一个只使用片段的一个实例,一个使用两个,依此类推。主 XML 布局文件现在有一个FrameLayout
点,当模式更改时,我会替换相应的“包含”XML 布局文件。
这是执行更改的代码片段:
private void changeLayout(int layoutID) {
FragmentManager fMgr = getFragmentManager();
FragmentTransaction transaction = fMgr.beginTransaction();
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
FrameLayout frame = (FrameLayout) findViewById(R.id.paletteContainer);
frame.removeAllViews();
View view = inflater.inflate(layoutID, frame, false);
frame.addView(view);
transaction.commit();
pFrag = (SwatchFragment.Primary) fMgr.findFragmentById(R.id.base);
a1Frag = (SwatchFragment.FirstAlternate) fMgr.findFragmentById(R.id.alternate1);
a2Frag = (SwatchFragment.SecondAlternate) fMgr.findFragmentById(R.id.alternate2);
cFrag = (SwatchFragment.Complementary) fMgr.findFragmentById(R.id.complementary);
}
它例外如下,因为一个或多个片段正在被重用。那么替换包含片段的部分 UI 的正确方法是什么?
E/WheelActivityandroid.view.InflateException: Binary XML file line #11: Error inflating class Navigation item selected: pos=1, id=0x00000001
android.view.InflateException: Binary XML file line #11: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) ~[na:0.0]
at android.view.LayoutInflater.rInflate(LayoutInflater.java:746) ~[na:0.0]
at android.view.LayoutInflater.inflate(LayoutInflater.java:489) ~[na:0.0]
at android.view.LayoutInflater.inflate(LayoutInflater.java:396) ~[na:0.0]
at org.dobbo.colour.activity.WheelActivity.changeLayout(WheelActivity.java:321) ~[na:0.0]
at org.dobbo.colour.activity.WheelActivity.setMode(WheelActivity.java:307) ~[na:0.0]
at org.dobbo.colour.activity.WheelActivity.onNavigationItemSelected(WheelActivity.java:181) ~[na:0.0]
at com.android.internal.widget.ActionBarView$1.onItemSelected(ActionBarView.java:148) ~[na:0.0]
at android.widget.AdapterView.fireOnSelected(AdapterView.java:892) ~[na:0.0]
at android.widget.AdapterView.access$200(AdapterView.java:49) ~[na:0.0]
at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:860) ~[na:0.0]
at android.os.Handler.handleCallback(Handler.java:725) ~[na:0.0]
at android.os.Handler.dispatchMessage(Handler.java:92) ~[na:0.0]
at android.os.Looper.loop(Looper.java:137) ~[na:0.0]
at android.app.ActivityThread.main(ActivityThread.java:5039) ~[na:0.0]
at java.lang.reflect.Method.invokeNative(Native Method) ~[na:0.0]
at java.lang.reflect.Method.invoke(Method.java:511) ~[na:0.0]
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) ~[na:0.0]
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) ~[na:0.0]
at dalvik.system.NativeStart.main(Native Method) ~[na:0.0]
Caused by: java.lang.IllegalArgumentException: Binary XML file line #11: Duplicate id 0x7f0a000b, tag null, or parent id 0x7f0a0010 with anoth
er fragment for org.dobbo.colour.fragment.SwatchFragment$Primary
at android.app.Activity.onCreateView(Activity.java:4722) ~[na:0.0]
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680) ~[na:0.0]
...