Android fragments 导航抽屉和动态创建的片段冲突

Android fragments 导航抽屉和动态创建的片段冲突,android-fragments,Android Fragments,我在android studio中使用默认的导航抽屉,我有一个动态创建的片段,并使用TabLayout来运行它。它将正常运行,但当我试图从导航抽屉加载相同的片段时,我遇到了错误。请参阅下文: import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.support.v4.content.ContextCom

我在android studio中使用默认的导航抽屉,我有一个动态创建的片段,并使用TabLayout来运行它。它将正常运行,但当我试图从导航抽屉加载相同的片段时,我遇到了错误。请参阅下文:

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;



public class Introduction extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {


        final RelativeLayout relativeLayout = new RelativeLayout(getActivity());
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
        relativeLayout.setLayoutParams(layoutParams);

        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);

        final ImageView introImage = new ImageView(getActivity());
        introImage.setId(R.id.introImageId);
        introImage.setImageResource(R.drawable.introduction);

        introImage.setAnimation(AnimationUtils.loadAnimation(this.getContext(), android.R.anim.slide_out_right));

        introImage.setPadding(100, 100, 100, 100);

        final TextView introText = new TextView(getActivity());
        introText.setText(R.string.introduction_text);
       // introText.setTextAppearance(android.R.style.TextAppearance_Medium);
        introText.setTextSize(20);
        introText.setPadding(25,25,25,25);
        introText.setTextColor(ContextCompat.getColor(this.getContext(), R.color.colorPrimary));
        params.addRule(RelativeLayout.BELOW, introImage.getId());
        introText.setAnimation(AnimationUtils.loadAnimation(this.getContext(),android.R.anim.slide_in_left));
        relativeLayout.addView(introText,params);
        relativeLayout.addView(introImage);


        return relativeLayout;

    }

}
但当我试图从导航抽屉加载它时,我得到一个错误:错误:(115,62)错误:不兼容的类型:简介无法转换为片段

 public boolean onNavigationItemSelected(MenuItem item) {

        FragmentManager fn = getFragmentManager();

        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_introduction) {

            fn.beginTransaction().replace(R.id.content_frame,new Introduction()).commit();

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
斑翅目


我替换了getFragmentManager();使用getSupportFragmentManager();及其工作原理

 // FragmentManager fn = getFragmentManager();
            FragmentManager fn = getSupportFragmentManager();
 // FragmentManager fn = getFragmentManager();
            FragmentManager fn = getSupportFragmentManager();