I am working on my first android app so forgive me if this is a simple question. The app currently has five tabs (made using fragments) and I am currently trying to set up a ListFragment in the first tab.
In my MainActivity class I have the code:
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
if (tab.getPosition() == 0) {
TopRatedListFragment topRatedListFragment = new TopRatedListFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.container, topRatedListFragment).commit();
} else {
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, tab.getPosition() + 1);
fragment.setArguments(args);
getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment).commit();
}
Now this works fine for my “DummySectionFragment”s. But my TopRatedListFragment extends ListFragment instead of just Fragment… so therefore the above code is giving me issues.
The line:
getSupportFragmentManager().beginTransaction().replace(R.id.container, topRatedListFragment).commit();
is the one drawing the error and saying the .replace method doesn’t like the topRatedListedFragment argument since it is a ListFragment and not a regular Fragment.
How should I fix this?
Thanks!
Most likely, your imports and/or class definitions are messed up.
There are two implementations of
Fragment,ListFragment, andFragmentTransaction. One set is inandroid.app. The other set is inandroid.support.v4.app. These are not compatible. You use the latter pair if you are usingFragmentActivityfor the Android Support package’s backport of fragments.My guess is that your
FragmentTransactionand yourListFragmentare coming from different packages, one fromandroid.appand one fromandroid.support.v4.app. If so, you need to fix that, having both use the same package, whichever one is appropriate for your circumstances.