0

I have an activity that has a ViewPager in the layout.

I have two fragments which display, one for each tab.

One of the fragments is designed to host other fragments - this is the CustomerMainFragment which inflates fragment_customer_main:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/lyt_customer_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <FrameLayout
        android:id="@+id/fragment_customer_content"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</LinearLayout>

This then adds CustomerSearchFragment to the FrameLayout which inflates fragment_customer_search.

CustomerSearchFragment also has the following override to switch out the search fragment for a detail fragment on a button press:

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    Button btnSearch1 = (Button) view.findViewById(R.id.button1);
    if (btnSearch1 != null) {
        btnSearch1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // This is important bit
                Fragment customerDetailFragment = new CustomerDetailFragment();
                FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
                transaction.addToBackStack(null);
                transaction.replace(R.id.fragment_customer_content, customerDetailFragment).commit();

            }
        });
    }
}

After clicking the button I get the following error:

java.lang.IllegalArgumentException: No view found for id 0x7f080006 (com.chrisbeckyapps.sample:id/fragment_customer_content) for fragment CustomerDetailFragment{4280b0b8 #0 id=0x7f080006}

I'm new to fragments and understand the concepts, but I'm stumped by this. I originally had the search fragment going straight into the pager, but then replacing it with the detail fragment mean it just showed over the top, and my research led to this being a better solution.

I have wondered about trying to move the search logic to the CustomerMainFragment but this means hooking up a lot of logic and I thought you could embed logic within fragments.

Suggestions?

4

1 回答 1

4

抱歉,刚刚找到这么简单的解决方法。

在我的 onclick 处理程序中,我只需要从更改getChildFragmentManagergetFragmentManager

于 2013-08-23T21:16:06.537 回答