title: Adding Fragment to Android
body: <p>T'm trying a very simple thing. Add a fragment through java code. But it is showing me error in (fragmentTransaction.add(R.id.fragment_container, fragment);). I'm not able to understand what is wrong over here. Please help. There are two classes the main class and the fragment class. and two layout the main layout and the fragment layout.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;android.support.constraint.ConstraintLayout 
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context="com.example.chetan.fragmentprtc.MainActivity"&gt;
  
  
  &lt;fragment
      android:id="@+id/fragment_container"
      android:name="android.app.DialogFragment"
      android:layout_width="368dp"
      android:layout_height="495dp"
      tools:layout_editor_absoluteX="8dp"
      tools:layout_editor_absoluteY="8dp" /&gt;
  
  &lt;/android.support.constraint.ConstraintLayout&gt;
  
  
  package com.example.chetan.fragmentprtc;
  import android.app.FragmentManager;
  import android.app.FragmentTransaction;
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  
  public class MainActivity extends AppCompatActivity {
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      FragmentManager fragmentManager = getFragmentManager();
      FragmentTransaction fragmentTransaction = 
  fragmentManager.beginTransaction();
      BlankFragment fragment = new BlankFragment();
      fragmentTransaction.add(R.id.fragment_container, fragment);
      fragmentTransaction.commit();
  }
  }
  
  
  
  
  &lt;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"
  tools:context="com.example.chetan.fragmentprtc.BlankFragment"&gt;
  
  &lt;!-- TODO: Update blank fragment layout --&gt;
  &lt;TextView
      android:id="@+id/fragment1"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:text="@string/hello_blank_fragment" /&gt;
  
  &lt;/FrameLayout&gt;
  
  package com.example.chetan.fragmentprtc;
  
  
  import android.os.Bundle;
  import android.support.v4.app.Fragment;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  
  public class BlankFragment extends Fragment {
  
  
  public BlankFragment() {
      // Required empty public constructor
  }
  
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_blank,container,false);
      return view;
  
  }
  
  }
  </code></pre>