gpt4 book ai didi

android - 在单个 Activity 中的 Fragment 之间切换

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:53:27 25 4
gpt4 key购买 nike

我想创建一个 Activity,它显示一种用户可以浏览的菜单。通过单击一个项目,将显示一个新屏幕,为用户提供更多选项(类似于向导)。

我想使用 Fragment 来实现它,但它对我不起作用。
现在我有:

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>

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); //also crashes with R.id.main_fragmentcontainer
transaction.addToBackStack(null);
transaction.commit();
}

现在,当我按下按钮时,应用程序崩溃并显示此 logcat:

06-27 01:45:26.309: E/AndroidRuntime(8747): java.lang.IllegalStateException: Can't change container ID of fragment SecondMenuFragment{405e2a70 #1 id=0x7f060029}: was 2131099689 now 2131099687
06-27 01:45:26.309: E/AndroidRuntime(8747): at android.support.v4.app.BackStackRecord.doAddOp(Unknown Source)
06-27 01:45:26.309: E/AndroidRuntime(8747): at android.support.v4.app.BackStackRecord.replace(Unknown Source)
06-27 01:45:26.309: E/AndroidRuntime(8747): at android.support.v4.app.BackStackRecord.replace(Unknown Source)
06-27 01:45:26.309: E/AndroidRuntime(8747): at com.myapp.MainMenuFragment$MyButtonOnClickListener.onClick(MainMenuFragment.java:52)

我做错了什么?

最佳答案

我创建了这个主布局:

<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="horizontal"
tools:context="com.example.fragmentsexample.MainActivity" >

<FrameLayout
android:id="@+id/contentFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" />

</LinearLayout>

我在 FrameActivity 中补充了:

@Override
public void onCreate(Bundle savedInstanceState) {
...
Fragment fragment = new Dashboard();
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
transaction.replace(R.id.contentFragment, fragment);
transaction.commit();
...
}

然后我用相同的代码替换 onClick 方法,更改 Fragment(事件仪表板):

@Override
public void onClick.... {
...
Fragment fragment = new Events();
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
transaction.replace(R.id.contentFragment, fragment); //Container -> R.id.contentFragment
transaction.commit();
...
}

关于android - 在单个 Activity 中的 Fragment 之间切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11217551/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com