He began to study such a topic as fragments. I want to add a fragment to my container that I previously created.

package com.development.kernel.fragments; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class TestFragment extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment1, null); return view; } } 

And for it xml markup.

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="Hello" android:background="#ba33"/> </LinearLayout> 

And MainActivity:

 package com.development.kernel.fragments; import android.app.FragmentTransaction; import android.support.v4.app.Fragment; 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); Fragment test = new TestFragment(); FragmentTransaction ft = getFragmentManager().beginTransaction(); ft.add(R.id.frame_layout, test); //вот здесь возникает ошибка ft.commit(); } } 

And the main_activity markup:

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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" tools:context="com.development.kernel.fragments.MainActivity"> <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/frame_layout"> </FrameLayout> </RelativeLayout> 

I have commented where the error occurs.

Error - cannot resolve method add(int, android.support.v4.app.Fragment)

How can I fix this error?

    1 answer 1

    This code should work. The bottom line is that you have a coder not from the support library, but fragments from support. For proper operation, they must be of the same type.

      getSupportFragmentManager() .beginTransaction() .replace(containderId, fragmentInstance) .commit();