Reputation: 5525
I want to add a fragment into relative layout and here's my MainActivity.java :
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
FragmentOne f1 = new FragmentOne();
fragmentTransaction.add(R.id.fragment_container, f1);
fragmentTransaction.commit();
}
}
when I run that code, I got this error :
Error:(15, 61) error: incompatible types: android.app.FragmentManager cannot be converted to android.support.v4.app.FragmentManager
how to add fragment into relative layout? I tried to change getFragmentManager();
into getSupportFragmentManager();
also didn't work. any idea what's wrong with this?
UPDATE : here's what I got when I change getFragmentManager();
into getSupportFragmentManager();
Upvotes: 0
Views: 274
Reputation: 10165
Check your imports. You're importing android.support.v4.app.FragmentManager
, so the variable FragmentManager fragmentManager
is of type android.support.v4.app.FragmentManager
but getFragmentManager
returns the platform version of fragment manager android.app.FragmentManager
, so these are incompatible types.
Since you are using AppCompatActivity
, you should be using getSupportFragmentManager
.
You say
I tried to change getFragmentManager(); into getSupportFragmentManager(); also didn't work.
What does "also didn't work" mean? That should work.
Upvotes: 1