when adding fragment inside a fragment Error inflating class fragment

时间:2017-06-12 16:49:52

标签: android performance android-fragments

I have added the fragment inside the fragment i got the error below how to fix it. My java Fragment class is as follows

public class MyListFragment extends Fragment {

IntentFilter filter;
Toolbar mToolbar;

MyListAdapter mAdapterlist;

public static Connections newInstance(int instance) {
    Bundle args = new Bundle();
    args.putInt("argsInstance", instance);
    Connections fourthFragment = new Connections();
    fourthFragment.setArguments(args);

    return fourthFragment;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View root = inflater.inflate(R.layout.fragment_my_list, container, false);

    mToolbar = (Toolbar) root.findViewById(R.id.toolbar1);
    //  mToolbar.setBackgroundColor(getResources().getColor(R.color.white));
    //   mToolbar.setTitleTextColor(getResources().getColor(R.color.menu));
     mToolbar.setTitle("");
    ((AppCompatActivity)getActivity()).getDelegate().setSupportActionBar(mToolbar);


//add this line if you want to provide Up Navigation but don't forget to to
//identify parent activity in manifest file

    ((AppCompatActivity)getActivity()).getDelegate().getSupportActionBar().setDisplayShowTitleEnabled(false);
    setHasOptionsMenu(true);
    ;
/*
    SharedPreferences sharedPreferences = getContext().getSharedPreferences("reg_id", Context.MODE_PRIVATE);
    String user_id = sharedPreferences.getString("id","");
    My_listing obj = new My_listing(user_id);
    obj.execute();*/



        if (getActivity() == null)
            return null;
        FragmentTransaction ft = getChildFragmentManager().beginTransaction();
        Trends nf = (Trends) Trends.instantiate(getActivity(), Trends.class.getName(),savedInstanceState);
        ft.add(R.id.fragment3, nf);
        ft.commit();


    RadioButton trends1 = (RadioButton) mToolbar.findViewById(R.id.trending1);
    RadioButton mine1   = (RadioButton) mToolbar.findViewById(R.id.mine1);

    final LinearLayout fragment4layout = (LinearLayout) root .findViewById(R.id.fragment4layout);

    mine1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {



        }
    });

    trends1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            fragment4layout.setVisibility(View.GONE);

        }
    });

    return root;

}

and the layout file is:

 <FrameLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.ksoft.propreka.Fragment.MyListFragment">

<fragment
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
   android:id="@+id/fragment3" />

<LinearLayout
    android:orientation="vertical"
    android:id="@+id/fragment4layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay"
    android:id="@+id/view5" >
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar1"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:subtitleTextColor="@color/colorPrimaryDark"
        app:popupTheme="@style/AppTheme.PopupOverlay"
        android:layout_gravity="center_horizontal|top">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toLeftOf="@+id/textView2"
            android:id="@+id/linearLayout2"
            android:weightSum=".5"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="false"
            android:layout_alignParentRight="false"
            android:layout_centerVertical="true"
            android:gravity="left">

            <RadioGroup
                android:checkedButton="@+id/offer"
                android:id="@+id/toggle1"
                android:layout_width="wrap_content"
                android:layout_height="30dp"
                android:background="@drawable/blue_out_line"
                android:foregroundGravity="right"
                android:orientation="horizontal"
                android:gravity="left"
                android:layout_gravity="left|top"
                android:layout_below="@+id/linearLayout2"
                android:layout_alignRight="@+id/linearLayout2"
                android:layout_alignEnd="@+id/linearLayout2"
                android:weightSum="1">

                <RadioButton
                    android:id="@+id/trending1"
                    android:background="@drawable/toggle_widget_background"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:button="@null"
                    android:text="Trending"
                    android:textColor="@color/colorPrimaryDark"
                    android:gravity="center"
                    android:layout_weight=".5" />

                <RadioButton
                    android:id="@+id/mine1"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:background="@drawable/toggle_widget_background"
                    android:button="@null"
                    android:gravity="center"
                    android:text="Mine"
                    android:textColor="@color/colorPrimaryDark"
                    android:layout_weight=".5" />
            </RadioGroup>
        </LinearLayout>
           <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Search Listing"
                android:layout_centerVertical="true"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:textSize="15sp" />

  </android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
    android:id="@+id/myList"
    android:layout_width="match_parent"
    android:layout_height="387dp"
    android:paddingTop="10dp"
    android:layout_gravity="right"
    android:layout_marginTop="60dp"/>
</LinearLayout>

and i get these error messages on the LogCat Window:

 06-12 22:13:26.999 29873-29873/com.ksoft.propreka E/AndroidRuntime: FATAL EXCEPTION: main
                                                                android.view.InflateException: Binary XML file line #8: Error inflating class fragment
                                                                    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
                                                                    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
                                                                    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
                                                                    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
                                                                    at com.ksoft.propreka.Fragment.MyListFragment.onCreateView(MyListFragment.java:68)
                                                                    at android.support.v4.app.Fragment.performCreateView(Fragment.java:2074)
                                                                    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1061)
                                                                    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1264)
                                                                    at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1366)
                                                                    at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2409)
                                                                    at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
                                                                    at android.view.LayoutInflater$FactoryMerger.onCreateView(LayoutInflater.java:168)
                                                                    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:675)
                                                                    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
                                                                    at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
                                                                    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
                                                                    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
                                                                    at com.ksoft.propreka.Fragment.Trends.onCreateView(Trends.java:83)
                                                                    at android.support.v4.app.Fragment.performCreateView(Fragment.java:2074)
                                                                    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
                                                                    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1286)
                                                                    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:758)
                                                                    at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1632)
                                                                    at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:637)
                                                                    at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:166)
                                                                    at android.support.v4.view.ViewPager.populate(ViewPager.java:1235)
                                                                    at android.support.v4.view.ViewPager.populate(ViewPager.java:1083)
                                                                    at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1609)
                                                                    at android.view.View.measure(View.java:15523)
                                                                    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
                                                                    at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:668)
                                                                    at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:735)
                                                                    at android.view.View.measure(View.java:15523)
                                                                    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
                                                                    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
                                                                    at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
                                                                    at android.view.View.measure(View.java:15523)
                                                                    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
                                                                    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
                                                                    at android.view.View.measure(View.java:15523)
                                                                    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
                                                                    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
                                                                    at android.view.View.measure(View.java:15523)
                                                                    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
                                                                    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
                                                                    at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
                                                                    at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
                                                                    at android.view.View.measure(View.java:15523)
                                                                    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
                                                                    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
                                                                    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
                                                                    at android.view.View.measure(View.java:15523)
                                                                    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
                                                                    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
                                                                    at android.view.ViewRootI

ho to fix this issue

1 个答案:

答案 0 :(得分:0)

在您的布局xml中,FrameLayout只能容纳一个孩子,因此您应该将其更改为RelativeLayoutLinearLayout。将您的fragment3更改为FrameLayout,将其用作碎片的容器。