-1

This is Main Activity XML

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:background="#71b7d6"
        tools:context="com.csdelta.haroon.fragmentpractice.MainActivity">
<!--Binary XML file line #13  --> 
 <fragment    
        android:id="@+id/my_frag"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:name="com.csdelta.haroon.fragmentpractice.MyFragment"/>
    </LinearLayout>

This is Fragment Activity XML

    <LinearLayout 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"
        android:background="#f7dc0f"
        tools:context="com.csdelta.haroon.fragmentpractice.MyFragment">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Hello, This is Fragment"
            android:textSize="25dp"
            android:layout_margin="20dp"
            android:textColor="#000"
            />

    </LinearLayout>

This is Main Activity Java File

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d("Haroon", "onCreate");
        setContentView(R.layout.activity_main);

    }
}

Here is Fragment Java File

public class MyFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Log.d("Haroon", "onCreateView");
        return inflater.inflate(R.layout.fragment_fragment_layout, container, false);
    }
}

The logcat message which i get says:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.csdelta.haroon.fragmentpractice/com.csdelta.haroon.fragmentpractice.MainActivity}: android.view.InflateException: Binary XML file line #13: Binary XML file line #13: Error inflating class fragment Caused by: android.view.InflateException: Binary XML file line #13: Binary XML file line #13: Error inflating class fragment Caused by: android.view.InflateException: Binary XML file line #13: Error inflating class fragment Caused by: android.app.Fragment$InstantiationException: Trying to instantiate a class com.csdelta.haroon.fragmentpractice.MyFragment

that is not a Fragment

Here is the complete LogCat: https://pastebin.com/iss7ui16

  • in your case you should use `FragmentActivity` instead of `Activity` – x0r Jun 11 '17 at 11:18
  • Possible duplicate of [Android SDK error: Trying instantiate a class that is not a fragment](https://stackoverflow.com/questions/24213756/android-sdk-error-trying-instantiate-a-class-that-is-not-a-fragment) – x0r Jun 11 '17 at 11:19
  • Thank You very much. It worked by extending to FragmentActivity instead of Activity. – Haroon Khan Jun 11 '17 at 11:51

4 Answers4

0

I think your fragment extending support Fragment, so your activity need to extend AppCompatActivity and not Activity.

Alex
  • 9,102
  • 3
  • 31
  • 35
0

Covert fragment to FrameLayout tag like that

<FrameLayout   
    android:id="@+id/my_frag"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:name="com.csdelta.haroon.fragmentpractice.MyFragment"/>
Shivam Sharma
  • 290
  • 2
  • 14
  • It's still not working.Actually, I want to add fragment using XML. Following the actually procedure but don't what's the issue here. – Haroon Khan Jun 11 '17 at 11:49
0

If you get the same error, just try FrameLayout as

   <FrameLayout
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="70dp"/>

and call the fragment as

    getSupportFragmentManager().beginTransaction().replace(R.id.container, new MyFragment()).commit();
0

Change your MainActivity to this

public class MainActivity extends FragmentActivity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    FragmentManager manager = getSupportFragmentManager();
    FragmentTransaction transaction = manager.beginTransaction();
    transaction.replace(R.id.my_frag, new MyFragment()).commit();
 }
}
Adithya
  • 1,688
  • 1
  • 10
  • 18