无法解析FragmentTransaction.add()方法.为什么我收到此错误?

Nir*_*van 0 android android-fragments fragmenttransaction

我已经查看了其他答案,但没有一个能帮助我.我确保使用import android.support.v4.app.Fragment;,这不是问题.

MainActivity.java

     package com.example.nirvan.fragmentsexample2;


    import android.support.v4.app.FragmentActivity;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentTransaction;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;

    public class MainActivity extends FragmentActivity {

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

        myFragment myfragment=new myFragment();
        FragmentTransaction fragmentTransaction=getSupportFragmentManager().beginTransaction();
        fragmentTransaction.add(R.id.fragmentContainer,myfragment);
        fragmentTransaction.commit();

    }
   }
Run Code Online (Sandbox Code Playgroud)

错误:

Error:(20, 28) error: no suitable method found for add(int,myFragment)
method FragmentTransaction.add(Fragment,String) is not applicable
(argument mismatch; int cannot be converted to Fragment)
method FragmentTransaction.add(int,Fragment) is not applicable
(argument mismatch; myFragment cannot be converted to Fragment)
Run Code Online (Sandbox Code Playgroud)

我为什么要这个?

activity_main.xml中我有一个FrameLayout

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ff0000"
    android:id="@+id/fragmentContainer">

</FrameLayout>
Run Code Online (Sandbox Code Playgroud)

myFragment.java

package com.example.nirvan.fragmentsexample2;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;    
import android.view.View;
import android.view.ViewGroup;
public class myFragment extends Fragment
{
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle 
 savedInstanceState)
{
    return inflater.inflate(R.layout.fragment,container,false);
}

public myFragment(){}
}
Run Code Online (Sandbox Code Playgroud)

fragment.xml之

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="wrap_content"
    android:layout_centerInParent="true"
    android:paddingLeft="145dp"
    android:text="fragment One"/>

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

Int*_*iya 5

马虎的错误.你应该打电话v4.app.Fragment而不是 app.Fragment.

打开myFragment.java

import android.app.Fragment;
Run Code Online (Sandbox Code Playgroud)

import android.support.v4.app.Fragment;
Run Code Online (Sandbox Code Playgroud)