androidandroid-fragmentstabactivity

Use one Fragment for one Tab in TabActivity


Currently I am trying to programm an android app. I would love to have there a Tab Activity where I can use one Fragment for one Tab. I have tried to follow this Tutorial, but Android Studio says:

Incopatible Types: 
        Required: android.support.v4.app.Fragment
        Found: de......fragment_nameOfTheFragment

Code of my Function in the Activity + import statements:

import android.support.design.widget.TabLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import de.noname.MYNAME.APPNAME.fragment_geocache_details;
import de.noname.MYNAME.APPNAME.fragment_geocache_details_waypoints;

@Override
public Fragment getItem(int position) {
    switch (position) {
        case 0:
            return new fragment_geocache_details_waypoints (); // Here is one fail
        case 1:
            return new fragment_geocache_details(); // Here is the other fail
    }
}

If more code is required then I will edit my Post. But I copied the code from the tutorial and just changed the names to my ones.


Solution

  • You do not use the correct import into the de......fragment_nameOfTheFragment

    I think you have

    import android.app.Fragment
    

    and you need

    import android.support.v4.app.Fragment;
    

    EDIT

    @Override
    public Fragment getItem(int position) {
        Fragment f = null;
        switch (position) {
            case 0:
                f =  new fragment_geocache_details_waypoints (); // Here is one fail
                break; 
            case 1:
                f = new fragment_geocache_details(); // Here is the other fail
                break;
        }
        return f;
    }
    

    or

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                return new fragment_geocache_details_waypoints (); // Here is one fail
            case 1:
                return new fragment_geocache_details(); // Here is the other fail
            default:
                //Do something by default like throw an exception
        }
    }