Question

EDIT :

After the recommendation of using TabStrip instead, I have been searching for it and I decided to used it instead of I was trying to do. So the problem is still the same but different code: In my navigation drawer I have a switch, which gives a Fragment fragment a Fragment value, and when I extend my TodayFragment with FragmentActivity I can´t make the last assignment because it throws a type mismatch error.

So here is part of the code (TodayFragment is now TodayFrag because I'm trying things without erasing classes):

Fragment fragment = null;
switch (position) {
case 0:
    fragment = new HomeFragment();
    break;
case 1:
    fragment = new TimetablesFragment();
    break;
case 2:
    fragment = new SubjectsFragment();
    break;
case 3:
    fragment = new TodayFrag();
    break;
case 4:
    fragment = new Others2Fragment();
    break;

default:
    break;
}

And now I have my PagerTabStrib from http://blog.pboos.ch/android-pagertabstrip-viewpager/ class:

public class TodayFrag extends FragmentActivity {}

ORIGINAL QUESTION:

I have my navigation drawer done, and in a certain point, i switch a position variable to determine which fragment I need to set in the view:

Fragment fragment = null;
switch (position) {
case 0:
    fragment = new HomeFragment();
    break;
case 1:
    fragment = new OneFragment();
    break;
case 2:
    fragment = new TwoFragment();
    break;
case 3:
    fragment = new TodayFragment();
    break;
case 4:
    fragment = new Others2Fragment();
    break;

default:
    break;
}

All fragments are classes that just extends Fragment except for TodayFragment(). TodayFragment() extends Fragment and implements ActionBar.TabListener because I'm trying to achieve a tab swipeable view in one of the fragments of the Navigation drawer:

public class TodayFragment extends Fragment implements ActionBar.TabListener {}

The problem comes in the first part of the code, it says "Type mismatch: cannot convert from TodayFragment to Fragment"

I don't know too much Java, and I'm stuck.

I hope someone could help

Thanks in advance

Was it helpful?

Solution

As said Myanimal in his answer's comments, I think you have imported different classes of Fragment.

  • this android.app.Fragment
  • and this android.support.v4.app.Fragment

The ActionBar.TabListener you implemented or the TabStrip you are using are not the causes of the issue.

When an A class extends/implements a B class, the A class (the subclass) has the "same" properties as B (the base class):

public class A extends B ==> B b = new A(); is possible.

So try to remove all your imports and do CtrlShiftO to organize the imports. Then chose the same class of Fragments. The issue may disappear.

OTHER TIPS

A better approach for tabs in a Fragment is to use a combination of ViewPager and PagerTabStrip as per the example at http://blog.pboos.ch/android-pagertabstrip-viewpager/

Keep in mind that ViewPager is from the support library, so you'll also have to use Fragments from the support library.


Original (incorrect) answer:

You need to cast your TodayFragment to a Fragment:

fragment = (Fragment) new TodayFragment();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top