与碎片不兼容的类型错误

时间:2014-11-26 18:28:08

标签: java android android-fragments android-activity

我试图在我的主要活动中添加一个片段。当我输入fragment = new CrimeFragment();时,出现Incompatible types错误。

我尝试将new CrimeFragment();投射到Fragment,但这不起作用。

package com.adamanteusstudios.officesmostwanted;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;


public class MostWantedActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_most_wanted);

        //getting fragmentManager
        FragmentManager fm = getSupportFragmentManager();
        Fragment fragment = fm.findFragmentById(R.id.fragmentContainer);

        if(fragment == null){
            ***fragment =  new OffenseFragment();***
            fm.beginTransaction().add(R.id.fragmentContainer, fragment).commit();
        }
    }



}

错误说:

  

不兼容的类型
  必需:android.v4.support.app.Fragment
  发现:com.adamanteusstudios.officesmostwanted.OffenseFragment

如何解决此错误?

1 个答案:

答案 0 :(得分:3)

在课程OffenseFragment.java中替换

import android.app.Fragment;

import android.support.v4.app.Fragment;