Google maps api v2 - The type android.app.Fragment cannot be resolved. It is indirectly referenced from required .class files

Frank Milne picture Frank Milne · Dec 27, 2012 · Viewed 22.8k times · Source

I've found a few questions on this subject here but their answers do not seem to apply being that my project is referencing the library that contains the class. Here's my code:

 package com.demo.app;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class Mapview extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        GPSTracker gps = new GPSTracker(Mapview.this);

        // check if GPS enabled     
        if(gps.canGetLocation()){

            double latitude = gps.getLatitude();
            double longitude = gps.getLongitude();

            // \n is for new line
            // Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show(); 
        }else{
            // can't get location
            // GPS or Network is not enabled
            // Ask user to enable GPS/network in settings
            gps.showSettingsAlert();
        }

        double latitude = gps.getLatitude();
        double longitude = gps.getLongitude();


        SupportMapFragment fragment = new SupportMapFragment();
        getSupportFragmentManager().beginTransaction()
                .add(android.R.id.content, fragment).commit();

        GoogleMap mMap;
        mMap = ((MapFragment) getSupportFragmentManager().findFragmentById(android.R.id.content)).getMap();
        mMap.addMarker(new MarkerOptions()
                .position(new LatLng(0, 0))
                .title("Hello world"));


    }
}

So you can see that i referenced the Fragment library in the imports, Eclipse throws no errors so it seems to acknowledge that it exists. In in the image below you can see that my eclipse project there is shown referenced libraries. In it you can see the FragmentManager class with findFragmentById() available. I also found the other classes I'm referencing in a similar manner. So to conclude I have the libraries in a lib folder, and they are acknowledged by eclipse and throw no errors in the code but the package won't export due to errors and the only error is on line one of this code next to the "p" in package that states: 'The type android.app.Fragment cannot be resolved. It is indirectly referenced from required .class files'

referenced libraries

Any ideas?

I did try clean up and rebuild of paths a few times.

Answer

Joe Simpson picture Joe Simpson · Dec 27, 2012

Try changing

mMap = ((MapFragment) getSupportFragmentManager().findFragmentById(android.R.id.content)).getMap();

to

mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(android.R.id.content)).getMap();

You must be running a pre ICS version of Android and you are pulling in "MapFragment" which uses the built-in version of Fragments and not the support library version.