I'm trying to add a fragment to my main activity. When I type fragment = new CrimeFragment();
I get an Incompatible types
error.
I tried casting new CrimeFragment();
to a Fragment
, but that didn't work.
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();
}
}
}
The error says:
incompatible types
required: android.v4.support.app.Fragment
found: com.adamanteusstudios.officesmostwanted.OffenseFragment
How do I fix this error?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…