cannot cast 'android.support.v4.app.FragmentTransaction' to 'android.app.FragmentTransaction

网上的解决方法针对的都是把FragmentTransaction转化成V4包下的FragmentTransaction有问题
但是,我却遇到了相反的情况。
无法由V4包下的FragmentTransaction转化成app.FragmentTransaction

有没有高人来指点一下啊??
注:**extends android.support.v4.app.Fragment**这个千万不能改

package com.example.myslidepage.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.*;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.example.myslidepage.R;

/**
 * Created by lenovo on 4/30/2019.
 */

public class Fragment2 extends android.support.v4.app.Fragment implements View.OnClickListener {

    public Button btn_news,btn_contact,btn_message,btn_setting;
    public MyFragment f1,f2,f3,f4;
    @Nullable

    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {

        View view =inflater.inflate(R.layout.tab2,container,false);
        btn_news =  view.findViewById(R.id.btn_news);
        btn_contact =  view.findViewById(R.id.btn_contact);
        btn_message =  view.findViewById(R.id.btn_message);
        btn_setting = view.findViewById(R.id.btn_setting);

        btn_news.setOnClickListener(this);
        btn_contact.setOnClickListener(this);
        btn_message.setOnClickListener(this);
        btn_setting.setOnClickListener(this);

        //第一次初始化首页默认显示第一个fragment
        initFragment1();
        initFragment2();
        initFragment3();
        initFragment4();
        return view;
    }


    @Override
    public void onClick(View v) {
        if(v==btn_news){
            initFragment1();
        }
        else if(v==btn_message){
            initFragment2();
        }
        else if(v==btn_contact){
            initFragment3();
        }
        else{
            initFragment4();
        }
    }

    private void initFragment1(){
        android.support.v4.app.FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
        //开启事务,fragment的控制是由事务来实现的
        FragmentTransaction transaction=(FragmentTransaction)fragmentManager.beginTransaction();


        //第一种方式(add),初始化fragment并添加到事务中,如果为null就new一个
        if(f1 == null){
            f1 = new MyFragment("消息",R.drawable.message_selected);
            transaction.add(R.id.main_frame_layout, f1);
        }
        //隐藏所有fragment
        hideFragment(transaction);
        //显示需要显示的fragment
        transaction.show(f1);

        transaction.commit();
    }

    private void initFragment2(){
        android.support.v4.app.FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
        FragmentTransaction transaction=(FragmentTransaction)fragmentManager.beginTransaction();
        if(f2 == null){
            f2 = new MyFragment("消息",R.drawable.message_selected);
            transaction.add(R.id.main_frame_layout, f2);
        }
        //隐藏所有fragment
        hideFragment(transaction);
        //显示需要显示的fragment
        transaction.show(f2);
        transaction.commit();
    }
    private void initFragment3(){
        android.support.v4.app.FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
        FragmentTransaction transaction=(FragmentTransaction)fragmentManager.beginTransaction();

        if(f3 == null){
            f3 = new MyFragment("消息",R.drawable.message_selected);
            transaction.add(R.id.main_frame_layout, f3);
        }
        //隐藏所有fragment
        hideFragment(transaction);
        //显示需要显示的fragment
        transaction.show(f3);
        transaction.commit();
    }
    private void initFragment4(){
        android.support.v4.app.FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
        FragmentTransaction transaction=(FragmentTransaction)fragmentManager.beginTransaction();
        if(f4 == null){
            f4 = new MyFragment("消息",R.drawable.message_selected);
            transaction.add(R.id.main_frame_layout, f4);
        }
        //隐藏所有fragment
        hideFragment(transaction);
        //显示需要显示的fragment
        transaction.show(f4);
        transaction.commit();
    }


    //这个方法其实很巧,这个hide方法看一眼就是唬人的。这个方法将所有的fragment都hide了,因为他的判断条件是if(f1 != null)
//因为一开始初始化的时候,f1f2f3都已经初始化一遍了,所以不可能都为空,既然都不为空,那就都隐藏。然后点击哪个方法,那个再显示。
    private void hideFragment(FragmentTransaction transaction) {
        if(f1 != null){
            transaction.hide(f1);
        }
        if(f2 != null){
            transaction.hide(f2);
        }
        if(f3 != null){
            transaction.hide(f3);
        }
        if(f4 != null){
            transaction.hide(f4);
        }
    }


}

1个回答

import android.app.FragmentManager; 把这个导包删除
import android.app.FragmentTransaction; 把这个换成v4包

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问