Reputation: 6778
I'm having problem with different versions of Fragment related classes.
In Eclipse, in the Project Properties > Java Build Path > Libraries, I have:
android-support-v4.jar
android-support-v7-appcompart.jar
There are a few questions discussing similar problems, but they seem to have left me more confused. How do I fix this?
MainActivity.java
public class MainActivity extends FragmentActivity implements ActionBar.TabListener {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
....
mAdapter = new TabsPagerAdapter(this.getSupportFragmentManager());
Fragment myFragment = mAdapter.getItem(MY_TAB); // error here
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(resources.getInteger(R.id.pager), myFragment, resources.getString(R.string.my_fragment));
...
}
TabPageAdapter.java
public class TabsPagerAdapter extends FragmentPagerAdapter {
public TabsPagerAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
@Override
public Fragment getItem(int index) {
...
}
}
Edit (responding to comments). This is the confusion.
In MainActivity, I had:
import android.app.Fragment
Changing it to:
import android support.v4.app.Fragment;
causes this error:
The method add(int, android.app.Fragment, java.lang.String) in the type FragmentTransaction is not applicable for the arguments (int, android.support.v4.app.Fragment, java.lang.String)
on this line:
fragmentTransaction.add(resources.getInteger(R.id.pager), permitsFragment, resources.getString(R.string.mys_fragment));
Changing to use the support fragment manager causes this error:
Type mismatch: cannot convert from android.support.v4.app.FragmentManager to android.app.FragmentManager
on this line:
FragmentManager fragmentManager = getSupportFragmentManager();
So if I change all imports to v4:
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
then I get errors on onTabReselected, onTabSelected, onTabUnselected saying:
The method onTabUnselected(ActionBar.Tab, FragmentTransaction) of type MainActivity must override or implement a supertype method
There doesn't appear to a v4 version of ActionBar.
Upvotes: 0
Views: 2229
Reputation: 4222
I often get this and the most probable reason is wrong Fragment
import which happens if you create a Fragment using the inbuilt android studio method. (Right Click => New => Fragment or something similar)
You must be doing
import android.app.Fragment
Change that to
import android.support.v4.app.Fragment
and then use getSupportFragmentManager()
whenever dealing with fragments.
Upvotes: 0
Reputation: 266
You should either use Components from Support package or none.
So it applies to FragmentManager and Fragment.
Upvotes: 1