使用 getFragmentManager() 时类型不兼容

Incompatible types while using getFragmentManager()

我有以下代码:

package asus.example.com.notes1;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.ShareActionProvider;

public class MainActivity extends Activity {
private void selectItem(int position){
        Fragment fragment;
        switch (position){
            case 1:
                fragment = new PizzaFragment();
                break;
            case 2:
                fragment = new PastaFragment();
                break;
            case 3:
                fragment = new StoresFragment();
                break;
                default:
                    fragment = new TopFragment();
        }
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.replace(R.id.content_frame, fragment);
        ft.addToBackStack(null);
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        ft.commit();
    }
}

我不知道为什么,但 Android Studio 在以下行下划线:FragmentTransaction ft = getFragmentManager().beginTransaction(); 并写道:

incompatible types. Required: android.support.v4.app.FragmentTransaction Found: android.app.FragmentTransaction

试图改变:

android.support.v4.app.FragmentTransaction

在:

android.app.FragmentTransaction

但在这种情况下,它取消了以下行:

ft.replace(R.id.content_frame, fragment);

并写道:

Wrong 2nd arguement type

也许,这是因为扩展 Activity(也许 AppCompat 一切正常,但我无法更改,就好像我知道,我在我的应用程序中使用的主题不兼容与 Appcompat)

你用错了 android.support.v4.app.FragmentManager 那么你应该使用 getSupportFragmentManager()

需要改变

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment);