0

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.

2
  • 4
    change import from android.app.Fragment to android.support.v4.app.Fragment whereever you imported Fragment. Commented Apr 10, 2015 at 14:02
  • 4
    In addition, use the support versions. I.e.: getSupportFragmentManager() Commented Apr 10, 2015 at 14:05

2 Answers 2

1

You should either use Components from Support package or none.

So it applies to FragmentManager and Fragment.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.