Error: the add (int, Fragment, String) method in the FragmentTransaction type is not applicable for arguments (int, FragementTest, String)

I am trying to use Fragements. But in the file MainActivity.java transaction.add(R.id.my_layout, testfrag, ""); not initialized correctly. Find below the code I used.

 package com.example.testtabs; import android.os.Bundle; import android.app.Activity; import android.app.FragmentManager; import android.app.FragmentTransaction; import android.app.TabActivity; import android.view.Menu; import android.widget.TabHost; import android.support.v4.app.Fragment; import com.example.testtabs.R; public class MainActivity extends TabActivity { TabHost tab; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); FragementTest testfrag=new FragementTest(); FragmentManager manager=getFragmentManager(); FragmentTransaction transaction=manager.beginTransaction(); transaction.add(R.id.my_layout, testfrag, "");//Error is here } } 

activity_main.xml:

 <RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" android:id="@+id/my_layout" > 

 Error: The method add(int, Fragment, String) in the type FragmentTransaction is not applicable for the arguments (int, FragementTest, String) 

Fraragement.test

 package com.example.testtabs; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; /** * A simple {@link android.support.v4.app.Fragment} subclass. * */ public class FragementTest extends Fragment { public FragementTest() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_fragement, container, false); } } 
+8
android
source share
3 answers

changes:

 import android.app.FragmentManager; import android.app.FragmentTransaction; 

in

 import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; 

or ---

changes:

 import android.support.v4.app.Fragment; 

to

 import android.app.Fragment; 
+19
source share

The entire Fragment must be android.app.Fragment. If you want to use android.support.v4.app.Fragment, you must use getSupportFragmentManager () to get the manager.

+2
source share

His work for me:

Instead of using getFragmentManager, use getSupportFragmentManager ();

+1
source share

All Articles