android.support.v4.app.FragmentManager无法转换为android.app.FragmentManager

时间:2017-05-11 14:35:38

标签: java android android-fragments

我是Android的新手,我尝试在平板电脑中使用片段。但是我在MainActivity.java文件中遇到错误。

错误显示需要'android.app.FragmentManager'而不是'android.support.v4.app.FragmentManager'。我已经检查了库,它是由Android Studio自动导入的。

我该如何解决?非常感谢!

package com.example.fragmenttest;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button)findViewById(R.id.button);
        button.setOnClickListener(this);
        replaceFragment(new RightFragment());
    }
    @Override
    public void onClick(View v){
        switch (v.getId()){
            case R.id.button:
                replaceFragment(new AnotherRightFragment());
                break;
            default:
                break;
        }
    }
    public void replaceFragment(Fragment fragment){
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.right_fragment,fragment);
        transaction.commit();
    }
}

0 个答案:

没有答案