Android Backstack 与 Nested/Child 碎片
Android Backstack with Nested/Child Fragments
我对嵌套 Fragment 的回栈方式有疑问,如果能提供任何帮助,我将不胜感激。
我有片段 A 和片段 B。片段 A 包含另一个片段 (ButtonFragment)。在 Activity 的 onCreate 中,我加载片段 A,然后切换到片段 B。当我返回片段 A(从后台堆栈中取出)时,出现以下错误。
Exception dispatching finished signal.
E/MessageQueue-JNI(6330): Exception in MessageQueue callback:
handleReceiveCallback
E/MessageQueue-JNI(6330): android.view.InflateException: Binary XML
file line #16: Error inflating class fragment
...
Caused by: java.lang.IllegalArgumentException: Binary XML file line
16: Duplicate id 0x7f080000, tag null, or parent id 0x0 with another fragment for
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>
片段布局
<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>
MainAcitivity
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
片段布局
<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>
onCreate 片段 A
@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;
}
我认为此异常背后的原因是您的视图中有两个视图元素 id=@+id/button
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name" />
和
<fragment
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.example.fragmentnavigation.MainActivity$ButtonFragment"/>
尝试将其中一个重命名为唯一的名称
您必须在运行时添加子片段,而不是在 Fragment A 布局文件中的布局中声明它。我在实施 google 地图时遇到了同样的问题。
Caused by: java.lang.IllegalArgumentException: Binary XML file line 16: Duplicate id 0x7f080000, tag null, or parent id 0x0 with another fragment for com.example.fragmentnavigation.MainActivity$ButtonFragment
您布局中的两个控件具有相同的id,您需要更改其中任何一个控件的id
我对嵌套 Fragment 的回栈方式有疑问,如果能提供任何帮助,我将不胜感激。
我有片段 A 和片段 B。片段 A 包含另一个片段 (ButtonFragment)。在 Activity 的 onCreate 中,我加载片段 A,然后切换到片段 B。当我返回片段 A(从后台堆栈中取出)时,出现以下错误。
Exception dispatching finished signal.
E/MessageQueue-JNI(6330): Exception in MessageQueue callback: handleReceiveCallback
E/MessageQueue-JNI(6330): android.view.InflateException: Binary XML file line #16: Error inflating class fragment
...
Caused by: java.lang.IllegalArgumentException: Binary XML file line 16: Duplicate id 0x7f080000, tag null, or parent id 0x0 with another fragment for 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>
片段布局
<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>
MainAcitivity
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
片段布局
<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>
onCreate 片段 A
@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;
}
我认为此异常背后的原因是您的视图中有两个视图元素 id=@+id/button
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name" />
和
<fragment
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.example.fragmentnavigation.MainActivity$ButtonFragment"/>
尝试将其中一个重命名为唯一的名称
您必须在运行时添加子片段,而不是在 Fragment A 布局文件中的布局中声明它。我在实施 google 地图时遇到了同样的问题。
Caused by: java.lang.IllegalArgumentException: Binary XML file line 16: Duplicate id 0x7f080000, tag null, or parent id 0x0 with another fragment for com.example.fragmentnavigation.MainActivity$ButtonFragment
您布局中的两个控件具有相同的id,您需要更改其中任何一个控件的id