0

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.

ham-sandwich
  • 3,714
  • 10
  • 29
  • 43
school_guy
  • 188
  • 13

1 Answers1

1

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
    }
}
ThomasThiebaud
  • 9,015
  • 4
  • 43
  • 66
  • Ok, thanks I have solved this error, thanks for the fast reply, but now Android Studio says that the return statement is missing :( – school_guy Oct 13 '15 at 12:47
  • See edit (you function needs to return something. It can be null but it has to return someting. If you prefers you can add a `default` statement in your switch). – ThomasThiebaud Oct 13 '15 at 12:49
  • ok, Android Studio says it is ok, but when i try to open the screen with the tabs the app crashes with the following fail code at my console: java.lang.NullPointerException: Attempt to write to field 'android.support.v4.app.FragmentManagerImpl android.support.v4.app.Fragment.mFragmentManager' on a null object reference – school_guy Oct 13 '15 at 13:13
  • You can also read [this](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it). And mark this question as answered ! – ThomasThiebaud Oct 13 '15 at 13:15
  • OK, second solution works, what is the diffrence between the two solutions and what did you in general :D, i am a completly beginner like you will already have noticed :D – school_guy Oct 13 '15 at 13:19
  • In the first case if position is different from 0 or 1, the function will return null. So if you try to use it you will have a null pointer exception. In the second case, if the position is not 0 or 1, you will enter into the default statement where you can handle the error or return a default fragment. You should consider to follow a complete course. This [one](https://www.udacity.com/course/android-developer-nanodegree--nd801) is nice and free. – ThomasThiebaud Oct 13 '15 at 13:24
  • ok, thanks for your explanation, at the site they offer me courses where i have to pay for, i don't see any free content, but thank you anyway – school_guy Oct 13 '15 at 13:49
  • Go [here](https://www.udacity.com/course/developing-android-apps--ud853) and click on `Resume course material`. – ThomasThiebaud Oct 13 '15 at 13:51