-1

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

user1903
  • 790
  • 1
  • 6
  • 10

2 Answers2

0

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();
myanimal
  • 3,580
  • 26
  • 26
  • `TodayFragment` already extends `Fragment`, no need to cast it again ! – S.Thiongane May 04 '14 at 18:59
  • 1
    You're right. Not sure what I was thinking. He may be importing different ```Fragment``` classes in his activity and ```TodayFragment``` (native fragment vs support fragment). Just a guess though. – myanimal May 04 '14 at 19:04
  • Not that, I have tried that just in case, but it doesn't fix anything. I think the error can come because of the implements part, but not sure how to make it work – user1903 May 04 '14 at 19:20
  • I usually use a ```ViewPager``` with a ```PagerTabStrip``` when I want tabs in a Fragment. Not familiar with the ```ActionBar.TabListener```. – myanimal May 04 '14 at 19:33
  • could you give me more information about it? Maybe I can change it – user1903 May 04 '14 at 19:36
  • That example is really good, I think that even better than what I was trying to do. But I think that extending FragmentActivity will make that the app keeps throwing the same error :S – user1903 May 04 '14 at 20:11
  • @myanimal yes I think it's due to that. So please update your answer ! The downvotes will disapear automatically :) – S.Thiongane May 04 '14 at 21:00
0

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.

S.Thiongane
  • 6,883
  • 3
  • 37
  • 52
  • So, I did everything you said but I've changed the ActionBar.TabListener because I want to use TabStrip but this one uses FragmentActivity and I can't manage to make it work; I willedit and update the question so it can be understood easily. – user1903 May 05 '14 at 09:26