segfault
segfault

Reputation: 109

Weird Error inflating class fragment in Android

I just started learning ADT, and am stuck in creating Fragment. What I did follows the tutorial from http://developer.android.com/training/basics/fragments/creating.html .

However, when I run it, it gives me an "Error inflating class fragment in Android", and "app was stopped" shows on my mobile phone. I did try make MainActivity extend FragmentActivty, but it didn't work. Besides in the tutorial, it mentions if the minSdkVersion is greated or equal than 11, the activity using fragments only needs to extend Activity.

This is the ArticleFragment.java

package com.example.sample3;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class ArticleFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.article_view, container, false);
    }
}

This is MainActivity.java:

package com.example.sample3;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;

public class MainActivity extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

And this is my main.xml layout for main_activity

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


    <fragment
              android:name="com.example.sample3.ArticleFragment"
              android:id="@+id/article_fragment"
              android:layout_width="wrap_content"
              android:layout_height="match_parent" />

</LinearLayout>


android:minSdkVersion="13"
android:targetSdkVersion="18"

I really hope you genius guys could take a minute to help me solve the problem. I appreciate it a lot.

Upvotes: 0

Views: 638

Answers (4)

Thein
Thein

Reputation: 3968

Try by using latest library

import android.support.v4.app.Fragment;

Use FragmentActivity to work with that instead of Activity

import import android.support.v4.app.FragmentActivity;

Upvotes: 0

user2807662
user2807662

Reputation: 69

It seems like you are missing the article_view xml file. This is needed by the Article Fragment...or is it available in your layouts?

Upvotes: 1

Sino
Sino

Reputation: 886

Change this 'import android.support.v4.app.Fragment' to 'android.app.Fragment' and try.

Upvotes: 0

Emmanuel
Emmanuel

Reputation: 13223

The Fragment you are extending is from the support library and you are not using FragmentActivity, so as of right now your code will not run. With your current configuration, you need to extend android.app.Fragment

Upvotes: 0

Related Questions