2

我在努力合并选项卡式ActionBar片段ActionBar选项卡 - 用细节片段替换列表片段

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

我在做什么是以下几点:

  • 的主要活动创造了三个选项卡
  • 选项卡(名单片段)填充三个片段,并呈现其列表
  • 有关点击列表我在主活动中写了一个回调,它接收被点击的项目的id,并在选项卡上交换带有详细片段的列表片段(至少它应该这样做)
  • 在该回调中,我创建了要显示的详细片段并尝试替换列表frag与它一起。

不幸的是我没有获得超越:

java.lang.IllegalArgumentException异常:未找到ID 0x7f040027 的片段CouponDetailFragment查看{44f4e310#1 ID = 0x7f040027}

我附问题结尾处的相关片段。任何帮助,高度赞赏。

这些都是相关的片段:

public class MyCouponActivity extends SherlockFragmentActivity implements CouponListFragment.Callbacks 

名单片段:

<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" /> 

交易的片段交换:

@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(); 
} 

回答

3

如果你定义XML片段, 它是固定的,你不能用FragmentTransaction中的另一个片段替换它。

最好的办法是用某种ViewGroup替换<fragment />。不要忘记,那么您必须使用FragmentTransaction在活动的onCreate(..)中添加CouponListFragment。

+0

就是这样 - 非常感谢你:) – 2012-08-03 06:34:42