Tou_Dou
2017-03-17 05:15
采纳率: 33.3%
浏览 6.9k

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

程序抛出错误: 不兼容的类型: android.app.Fragment无法转换为android.support.v4.app.Fragment

Error:(34, 47) 错误: 不兼容的类型: android.app.Fragment无法转换为android.support.v4.app.Fragment
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.

Compilation failed; see the compiler error output for details.

 package fragment.example.com.fragmenttest;

import android.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.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;
        }
    }
    private void replaceFragment(Fragment fragment) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.right_layout,fragment);
        transaction.commit();
    }
}

报错位置是 transaction.replace(R.id.right_layout,fragment);的fragment

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • liangtao2016 2017-03-17 06:58
    已采纳

    包导错了,把import android.app.Fragment;这个删了,重新导入android.support.v4.app.Fragment

    点赞 打赏 评论
  • fcwxin 2017-03-17 07:46

    你的fragment是app包的换成v4包试试

    点赞 打赏 评论
  • MakerGaoGao 2017-03-19 07:05

    1L,2L说的都没问题,看看方法的入参的包名 再看看你的包名 对照下就能发现问题了

    点赞 打赏 评论

相关推荐 更多相似问题