Android平台:3.1
我试图将一个片段从容器A移动到容器B.下面是完成此操作的代码:
private void reattach(int newContainerId, Fragment frag, String tag) {
if (frag == null || !frag.isAdded() || (frag.getId() == newContainerId)) { return; }
final FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.remove(frag); //stacco il frammento dal container A
ft.commit();
fm.executePendingTransactions();
ft = fm.beginTransaction();
ft.add(newContainerId, frag, tag); //attacco il frammento sul container D
ft.commit();
fm.executePendingTransactions();
}
Run Code Online (Sandbox Code Playgroud)
当我运行系统时,我得到以下IllegalStateException:
03-26 00:13:14.829: E/AndroidRuntime(30090): java.lang.RuntimeException: Unable to start activity ComponentInfo{eu.areamobile.apps.sfa/eu.areamobile.apps.sfa.activity.HomeActivity}: java.lang.IllegalStateException: Can't change container ID of fragment FragmentHomeController{408202a8 id=0x7f050010 HomeController}: was 2131034128 now …Run Code Online (Sandbox Code Playgroud) 我想创建一个Activity显示用户可以通过的菜单.通过单击项目,将显示一个新屏幕,允许用户提供更多选项(类似向导).
我想用Fragments 来实现它,但它对我不起作用.
现在我有:
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="@+id/main_fragmentcontainer" >
<fragment
android:id="@+id/mainmenufragment"
android:name="com.myapp.MainMenuFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<fragment
android:id="@+id/secondmenufragment"
android:name="com.myapp.SecondMenuFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
MainMenuFragment用OnClickListener:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.mainmenu, container, false);
setupButton(view);
return view;
}
/* Button setup code omitted */
@Override
public void onClick(View v) {
SherlockFragment secondRunMenuFragment = (SherlockFragment) getSherlockActivity().getSupportFragmentManager().findFragmentById(R.id.secondmenufragment);
FragmentTransaction transaction = getSherlockActivity().getSupportFragmentManager().beginTransaction();
transaction.replace(android.R.id.content, secondMenuFragment); …Run Code Online (Sandbox Code Playgroud) 以下是我希望我的应用程序在平板电脑上执行的操作.Fragment(0)有一些菜单会显示片段(1)...(n),如下所示:
-----------------
| | | | |
| | | | |
|(0)| X | X | X |
| | | | |
| | | | |
-----------------
becomes
-----------------
| | | | |
| | | | |
|(0)|(1)| X | X |
| | | | |
| | | | |
-----------------
and then
-----------------
| | | | |
| | | | |
|(0)|(2)|(1)| X |
| | | | |
| …Run Code Online (Sandbox Code Playgroud)