0

I've seen this question in various places across Stack Overflow, however none of the answers are managing to solve my problem. The usual answer seems to tell the app to import android.support.v4.app.Fragment;

When I import this method it comes up that it's unused so I'm unsure whether I need it at all.

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;

public class OpenStreetMap extends AppCompatActivity {

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_open_street_map);
    OpenStreetMapFragment osmFragment = new OpenStreetMapFragment();
    FragmentManager manager = getSupportFragmentManager();
    manager.beginTransaction()
            .add(R.id.osm_map, osmFragment)
            .addToBackStack(null)
            .commit();
}

}

XML

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/osm_map">

    <org.osmdroid.views.MapView android:id="@+id/map"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</LinearLayout>

OpenStreetMapFragment

public class OpenStreetMapFragment extends FragmentActivity implements MapEventsReceiver,  LocationListener, android.location.LocationListener, SensorEventListener {

static String mapQuestApiKey;
protected MapView map;
protected GeoPoint startPoint, destinationPoint;
protected ArrayList<GeoPoint> viaPoints;
protected Marker startMarker;
protected LocationManager mLocationManager;
protected DirectedLocationOverlay myLocationOverlay;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mapQuestApiKey = "xxxxxxxxxxxxxxxxxxxxxxx";
    map = (MapView) findViewById(R.id.map);
    map.setTileSource(TileSourceFactory.MAPNIK);
    map.setBuiltInZoomControls(true);
    map.setMultiTouchControls(true);
    mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    IMapController mapController = map.getController();
    mapController.setZoom(12);


    myLocationOverlay = new DirectedLocationOverlay(this);
    map.getOverlays().add(myLocationOverlay);

    if (savedInstanceState == null) {
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        Location location = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        GeoPoint geoPoint = new GeoPoint(location);
        mapController.setCenter(geoPoint);
        mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500, 0, this);

        startPoint = null;
        destinationPoint = null;
        viaPoints = new ArrayList<GeoPoint>();
    } else {
        myLocationOverlay.setLocation((GeoPoint) savedInstanceState.getParcelable("location"));
        //TODO: restore other aspects of myLocationOverlay...
        startPoint = savedInstanceState.getParcelable("start");
        destinationPoint = savedInstanceState.getParcelable("destination");
        viaPoints = savedInstanceState.getParcelableArrayList("viapoints");
    }
}


@Override public void onLocationChanged(final Location pLoc) {

    GeoPoint newLocation = new GeoPoint(pLoc);
    Log.e("Latitude", ":" + newLocation.getLatitude());
    Log.e("Longitude", ":" + newLocation.getLongitude());
    if (!myLocationOverlay.isEnabled()){
        //we get the location for the first time:
        myLocationOverlay.setEnabled(true);
        map.getController().animateTo(newLocation);

    }

    myLocationOverlay.setLocation(newLocation);
    String msg = "New Latitude: " + newLocation.getLatitude() + "New Longitude: " + newLocation.getLongitude();
    myLocationOverlay.setAccuracy((int)pLoc.getAccuracy());
    Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();

}

}

Alan Rochford
  • 65
  • 2
  • 5

3 Answers3

2

Thats because you have not used Fragment class in your code so far.

If you are not using Fragment class, you can remove

import android.support.v4.app.Fragment;`
Rakesh
  • 756
  • 1
  • 9
  • 19
0

The problem is that OpenStreetMapFragment is a FragmentActivity not a Fragment. You are trying to use it as a Fragment. From your code, I can't understand why you are trying to use OpenStreetMapFragment with OpenStreetMap. In my opinion, you can directly use OpenStreetMapFragment instead of OpenStreetMap.

I'll suggest you to first look at a difference betweeen Activity, FragmentActivity and Fragment. There are lots of questions that can be helpful. You can try this or this. Or go through documentation here or here. Or try this tutorial.

I hope it'll solve your problem. If you still have some queries, feel free to ask here.

Community
  • 1
  • 1
Rehan
  • 3,270
  • 5
  • 31
  • 30
  • Thanks a lot Rehan! I am directly using OpenStreetMapFragment and now I am getting the following error. I don't see how this is happening since I am not extending any Fragment. There are a lot of solutions on here but none of them worked for my project -------------- Unable to start activity ComponentInfo{alan.rochford.mydit.ie.walkthisway/alan.rochford.mydit.ie.walkthisway.OpenStreetMap}: android.view.InflateException: Binary XML file line #1: Error inflating class fragment – Alan Rochford Feb 17 '16 at 16:28
  • @AlanRochford Thats because of issues in your XML. Please update your question as per your current code so that I know where is the issue now! – Rehan Feb 18 '16 at 06:50
  • 1
    I have managed to fix it Rehan. There was a little problem in my Main Activity. Thanks for your help! – Alan Rochford Feb 18 '16 at 10:28
0

yeah simply add this

import android.support.v4.app.Fragment;

before to add this make sure all of your fragment related libraries should be remove. once you removed then add this one. and you will get rid from this error. hopefully it will help you.

Sajid Ali
  • 1
  • 2