12

Am getting Error inflating fragment error while running calling an Activity which having fragments.

CheckList is Activity which contains listview and Description fragments.and its xml code is:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="match_parent" android:layout_height="match_parent">

        <fragment class="com.uday.honeytest.CheckList$listview"
                android:id="@+id/titles" android:layout_weight="1"
                android:layout_width="0px" android:layout_height="match_parent" />

        <fragment class="com.uday.honeytest.CheckList$Description"
        android:id="@+id/details" android:layout_weight="1"
                android:layout_width="0px" android:layout_height="match_parent"
                android:background="?android:attr/detailsElementBackground" />

    </LinearLayout>

CheckList class is:

public class CheckList extends Activity {

    static int position=0;
    static CheckList check;

    public void onCreate(Bundle saved){

        super.onCreate(saved);
        setContentView(R.layout.checklist);


    }

    public static class listview extends ListFragment {

            View mConverView;
            String[] items;



             @Override
                public void onActivityCreated(Bundle savedInstanceState) {
                    super.onActivityCreated(savedInstanceState);
             }

            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                    Bundle savedInstanceState) {
                xxxxxx..
                     return mConverView;
            }
        }

similarly i have Description fragment below this .

But am getting

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.uday.honeytest/com.uday.honeytest.CheckList}: android.view.InflateException: Binary XML file line #9: Error inflating class fragment

error at setContentView line.

and my Description fragment class is: Edited:

public static class Description extends Fragment {

        View mConverView;
        String details[];

        public static Description getInstance() {
            Description desc=new Description();
            return desc;

        }

         @Override
            public void onActivityCreated(Bundle savedInstanceState) {
                super.onActivityCreated(savedInstanceState);
         }


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


            TextView textview=new TextView(getActivity());
            textview.setLayoutParams(new LayoutParams(MarginLayoutParams.WRAP_CONTENT, MarginLayoutParams.WRAP_CONTENT));



            return textview;
        }
    }

What am doing wrong here??

Thanks

Uday
  • 5,933
  • 9
  • 44
  • 76
  • You should really post your Description class code. – RoflcoptrException Aug 23 '11 at 14:15
  • Check my Description fragment class edited : which is in CheckList Activity only. – Uday Aug 23 '11 at 14:26
  • 2
    I've tried this example from here: http://www.vogella.com/articles/Android/article.html#fragments and I had 2 problems: - I needed to use `android.support.v4.app` and not `android.app.Fragment` because I am running under API level 8 (Android introduced fragments in Android 3.0 (API level 11)) - `android:layout_marginTop="?android:attr/actionBarSize"` in fragment declaration is not compatible with API level 8 - see http://stackoverflow.com/questions/7760817/how-to-get-androidattr-actionbarsize-with-compatibility-library – Paul Jun 13 '12 at 17:44

3 Answers3

8

Your activity CheckList should extend FragmentActivity .

spin.goran
  • 426
  • 3
  • 7
1

<fragment class="com.uday.honeytest.CheckList$Description" is where it's getting the error.

Are you sure you have the fully qualified class name typed correctly?

Have you tried cleaning your project/fixing project properties?

I don't see anything named Description in that class, that may be the issue.

Hope this helps.

Codeman
  • 12,157
  • 10
  • 53
  • 91
1

I just removed onCreateView code which contains inflating xml in listview fragmentand move into onActivityCreated method with that its working.

The reason may be:

ListFragment has a default layout that consists of a single list view. However, if you desire, you can customize the fragment layout by returning your own view hierarchy from onCreateView(LayoutInflater, ViewGroup, Bundle). To do this, your view hierarchy must contain a ListView object with the id "@android:id/list" (or list if it's in code)

So my listview extends ListFragment and not conatining list in xml , and directly we can setListAdapter in onActivityCreated rather than in onCreateView.

Uday
  • 5,933
  • 9
  • 44
  • 76
  • You may not have needed to do this. I notice in your example that you didn't have the @Override directive above your onCreateView method. Was it getting called at all? – Todd Grigsby Feb 28 '12 at 07:44
  • 1
    This is a bit misguiding as you must have gone with @spin.goran's answer – Prateek Jun 04 '13 at 12:58