I’m struggling combining a tabbed ActionBar and fragments.
I’m using ActionBarSherlock to display three independent tabs, and my problem relates to the first tab exclusively. It’s supposed to display items (called “coupons”) in a combination of list and detail view. The other two tabs display different content and do not play a role here.
What I’m doing is the following:
- The main activity creates three fragments for three tabs
- Tab A (a list fragment) populates and renders its list
- For clicks on the list I wrote a callback in the main activity that receives the clicked item’s id and exchanges the list fragment with the detail fragment on the tab (at least its supposed to do so)
- In that callback I create the to-be-displayed detail fragment and try to replace the list fragment with it.
Unfortunately I dont get beyond:
java.lang.IllegalArgumentException: No view found for id 0x7f040027
for fragment CouponDetailFragment{44f4e310 #1 id=0x7f040027}
I attached the relevant snippets at the end of the question. Any help is highly appreciated.
These are the relevant snippets:
public class MyCouponActivity extends SherlockFragmentActivity implements CouponListFragment.Callbacks
List 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" />
Transaction for fragment exchange:
@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();
}
If you define a fragment in XML, it is fixed and you can’t replace it with another fragment in a FragmentTransaction.
Your best bet would be to replace the
<fragment />with some sort ofViewGroup. Don’t forget then you’d have to use a FragmentTransaction to add a CouponListFragment in theonCreate(..)of your Activity.