android - ActionBar 选项卡 - 用详细信息 fragment 替换列表 fragment

标签 android android-fragments android-actionbar actionbarsherlock

我正在努力组合选项卡式ActionBar fragment

我使用 ActionBarSherlock 显示三个独立的选项卡,我的问题仅与第一个选项卡有关。它应该以列表和详细 View 的组合显示项目(称为“优惠券”)。另外两个选项卡显示不同的内容,在这里不起作用。

我正在做的事情如下:

  • 主要 Activity 为三个选项卡创建三个 fragment
  • 选项卡 A(列表 fragment )填充并呈现其列表
  • 对于列表上的点击,我在主 Activity 中编写了一个回调,用于接收点击项目的 id 并将列表 fragment 与选项卡上的详细信息 fragment 交换(至少应该这样做)
  • 在该回调中,我创建了要显示的详细信息 fragment ,并尝试用它替换列表 fragment 。

不幸的是我没有超越:

java.lang.IllegalArgumentException: No view found for id 0x7f040027 for fragment CouponDetailFragment{44f4e310 #1 id=0x7f040027}

我在问题末尾附上了相关 fragment 。非常感谢任何帮助。

这些是相关 fragment :

public class MyCouponActivity extends SherlockFragmentActivity implements CouponListFragment.Callbacks

列表 fragment :

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:name="foo.bar.CouponListFragment"
    android:id="@+id/coupon_list"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

fragment 交换交易:

@Override
public void onItemSelected(final String id) {
    final Bundle arguments = new Bundle();
    arguments.putString(CouponDetailFragment.ARG_ITEM_ID, id);
    final CouponDetailFragment fragment = new CouponDetailFragment();
    fragment.setArguments(arguments);
    getSupportFragmentManager().beginTransaction().replace(R.id.coupon_list, fragment).commit();
}

最佳答案

如果您在 XML 中定义 fragment ,则它是固定的,并且您无法将其替换为 FragmentTransaction 中的另一个 fragment 。

您最好的选择是替换 <fragment />与某种ViewGroup 。不要忘记,您必须使用 FragmentTransaction 在 onCreate(..) 中添加 CouponListFragment您的 Activity 。

关于android - ActionBar 选项卡 - 用详细信息 fragment 替换列表 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11782194/

相关文章:

java - 资源编译失败

Android:保存到 SD 卡总是在重新安装时删除

java - 我在使用 libgdx 首选项时遇到问题。不保存高分?

android - 试图将 map 放入 fragment Activity 中

java - 从 viewpager 中的 fragment 获取 edittext 值

android - 如何在 Fragments 中添加一个按钮

android - 如何只显示 "Action Bar Sherlock"片刻让用户知道它存在?

android - 在 Android 中调用 invalidateOptionsMenu() 时会发生什么?

android - Android 中 "Java class extends" "AppcompatActivity"与 "Activity"与 "ActionBar"之间的区别?

java - 如何让我的共享首选项正常工作?