我的代码中有这个:
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
public static class TheMapFrag extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_the_map, container,
false);
return rootView;
}
}
现在这些应该显示2个片段。
我的onCreate有两个标签:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Set up the action bar.
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab testTab = actionBar.newTab().setText("The test");
ActionBar.Tab chatTab = actionBar.newTab().setText("Chat");
testTab.setTabListener(this);
chatTab.setTabListener(this);
actionBar.addTab(testTab);
actionBar.addTab(chatTab);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
现在一切都很完美,但是当我使用以下代码更改标签时:
@Override
public void onTabSelected(Tab tab, FragmentTransaction arg1) {
int position = tab.getPosition();
switch(position){
case 0:
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
break;
case 1:
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new TheMapFrag()).commit();
break;
}
}
第一次有效,但当我更改为case 1
然后更改为case 0
然后又回到case 1
时,应用程序因这些错误而崩溃:
04-21 08:14:34.201: E/AndroidRuntime(2246): FATAL EXCEPTION: main
04-21 08:14:34.201: E/AndroidRuntime(2246): Process: com.gs.map, PID: 2246
04-21 08:14:34.201: E/AndroidRuntime(2246): android.view.InflateException: Binary XML file line #2: Error inflating class fragment
04-21 08:14:34.201: E/AndroidRuntime(2246): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
04-21 08:14:34.201: E/AndroidRuntime(2246): at android.view.LayoutInflater.inflate(LayoutInflater.java:469)
04-21 08:14:34.201: E/AndroidRuntime(2246): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
04-21 08:14:34.201: E/AndroidRuntime(2246): at com.gs.map.MainActivity$TheMapFrag.onCreateView(MainActivity.java:90)
04-21 08:14:34.201: E/AndroidRuntime(2246): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
04-21 08:14:34.201: E/AndroidRuntime(2246): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:938)
04-21 08:14:34.201: E/AndroidRuntime(2246): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
04-21 08:14:34.201: E/AndroidRuntime(2246): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
04-21 08:14:34.201: E/AndroidRuntime(2246): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
04-21 08:14:34.201: E/AndroidRuntime(2246): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:446)
04-21 08:14:34.201: E/AndroidRuntime(2246): at android.os.Handler.handleCallback(Handler.java:733)
04-21 08:14:34.201: E/AndroidRuntime(2246): at android.os.Handler.dispatchMessage(Handler.java:95)
04-21 08:14:34.201: E/AndroidRuntime(2246): at android.os.Looper.loop(Looper.java:136)
04-21 08:14:34.201: E/AndroidRuntime(2246): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-21 08:14:34.201: E/AndroidRuntime(2246): at java.lang.reflect.Method.invokeNative(Native Method)
04-21 08:14:34.201: E/AndroidRuntime(2246): at java.lang.reflect.Method.invoke(Method.java:515)
04-21 08:14:34.201: E/AndroidRuntime(2246): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-21 08:14:34.201: E/AndroidRuntime(2246): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-21 08:14:34.201: E/AndroidRuntime(2246): at dalvik.system.NativeStart.main(Native Method)
04-21 08:14:34.201: E/AndroidRuntime(2246): Caused by: java.lang.IllegalArgumentException: Binary XML file line #2: Duplicate id 0x7f060040, tag null, or parent id 0x7f06003f with another fragment for com.google.android.gms.maps.MapFragment
04-21 08:14:34.201: E/AndroidRuntime(2246): at android.app.Activity.onCreateView(Activity.java:4791)
04-21 08:14:34.201: E/AndroidRuntime(2246): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
04-21 08:14:34.201: E/AndroidRuntime(2246): ... 18 more
当我选择case 0
时,它会覆盖case 1
我知道我必须以某种方式隐藏或破坏显示的片段,但作为初学者,我无法实现它。我确定我错过了一些非常小的东西。
修改 fragment_main.xml代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.gs.map.MainActivity$PlaceholderFragment" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
答案 0 :(得分:1)
从错误中听起来你正试图在片段vi布局文件中添加片段。
但是你不能在片段布局文件中指定片段。
因此,如果要在片段内添加片段,则必须在运行时进行。
如何吗
1)从片段布局文件中删除<fragment>
标记。
2)将空白<LinearLyaout>
改为<fragment>
3)在运行时在该linearlayout中添加片段。
答案 1 :(得分:1)
我认为你正在给其他片段中的片段充气。
而不是
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new TheMapFrag()).commit();
使用
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, new TheMapFrag()).commit();
添加和替换refer this
之间的区别