我试图在我的主要活动中添加一个片段。当我输入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
如何解决此错误?
答案 0 :(得分:3)
在课程OffenseFragment.java
中替换
import android.app.Fragment;
与
import android.support.v4.app.Fragment;