【发布时间】:2015-05-18 14:56:09
【问题描述】:
我对嵌套片段的回栈方式有疑问,如果提供任何帮助,我将不胜感激。
我有 Fragment A 和 Fragment B。Fragment A 包含另一个 Fragment (ButtonFragment)。在 Activity 的 onCreate 中,我加载了 Fragment A,然后切换到 Fragment B。当我回到 Fragment A(退出后台堆栈)时,出现以下错误。
异常调度完成信号。
E/MessageQueue-JNI(6330):MessageQueue 回调中的异常: 处理接收回调
E/MessageQueue-JNI(6330):android.view.InflateException:二进制 XML 文件第 16 行:膨胀类片段时出错
...
原因:java.lang.IllegalArgumentException: Binary XML file line 16:重复 id 0x7f080000、标签 null 或父 id 0x0 与另一个片段 com.example.fragmentnavigation.MainActivity$ButtonFragment
如果没有子片段,导航就可以工作。也许我必须添加一些 ChildFragmentManager 处理,但我不知道什么和在哪里。我希望你能帮助我。
主布局
<LinearLayout 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:orientation="vertical" >
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name" />
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context="com.example.fragmentnavigation.MainActivity"
tools:ignore="MergeRootFrame" />
</LinearLayout>
片段A布局
<LinearLayout 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.example.fragmentnavigation.MainActivity$FragmentA" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<fragment
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.example.fragmentnavigation.MainActivity$ButtonFragment"/>
</LinearLayout>
主要活动
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new FragmentA()).commit();
}
}
...
private void switchToB() {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.container, new BFragment());
ft.addToBackStack(null);
ft.commit();
}
片段 A
public static class FragmentA extends Fragment {
public FragmentA() {
}
@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 ButtonFragment extends Fragment {
public ButtonFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.buttonfragment, container,
false);
return rootView;
}
}
解决方案特别感谢@arun-kumar
关于片段的一个非常好的概述 https://github.com/codepath/android_guides/wiki/Creating-and-Using-Fragments
片段A布局
<LinearLayout 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.example.fragmentnavigation.MainActivity$FragmentA" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<FrameLayout
android:id="@+id/child_fragment_container"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
片段A的onCreate
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
Fragment childFragment = new ButtonFragment();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.replace(R.id.child_fragment_container, childFragment).commit();
return rootView;
}
【问题讨论】:
标签: android android-fragments navigation android-nested-fragment fragment-backstack