I am trying to build an application using Google Maps API v2 but the thing is, the application keeps giving me force closes although i followed all the required instructions.
Presumably, you have not added a reference to the Google Play Services library project to your app project. See the Google Play Services SDK documentation, specifically the section entitled "To set up a project to use the Google Play services SDK".
I found you missed several things in your code.
Answer 1: Your GPSonMAP.java must be modified like below;
public class GPSonMap extends android.support.v4.app.FragmentActivity{
GoogleMap googlemap; // "MapView map;" is wrong
//MapController controller; ==> it was depreciated.
LocationManager locationManager;
LocationListener listener;
PendingIntent pendingIntent ;
Criteria criteria;
//MapOverlay overlay; ==> it was depreciated.
SupportMapFragment fm; // add this
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
Log.d("", "entered");
//map = (MapView)findViewById(R.id.mapView); ==> delete it.
fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapView);
googleMap = fm.getMap();
}
}
After modifying the above, press "Ctrl + Shft + (alphabet) O " and some required classes will be imported.
Answer 2: Your xml file in layout must be modified like below;
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".GPSonMap" >
Your MainActivity is not MapsActivity, but GPSonMap.
Answer 3: Your Manifest file does not match your package name with the described package name in the manifest file. Please match the package name with the same one.
Modify "com.example.example" to your package name.
After copying and pasting some codes, you MUST check the package name and class name at least.
I had this problem and was wasting time (over 1.5 days of hair pulling frustration). Then I watched a youtube video that was doing a boring walk through of the instructions to add the Google Play Services to an app. Well, in that video after adding the SDK from the sdk manager they used the Eclipse "Check for Updates". That little piece of magic went out and downloaded a jar file that the SDK Manager doesn't do! They really need to add this to the instructions and save people the frustration. I was tempted to just remove Google Maps from my app. I used the 1.0 and I needed to update to 2.0 or ditch maps.
HOW TO DISPLAY GOOGLE MAP via Google Maps API v2 on Android.
NOTE that All of two case are required to set the Google Play service and Manifest file including Several permissions and API key.
1. Display Google Map in (Support)Fragment.
main.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- This can go anywhere in your layout (see other demos for some examples). -->
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>
MainActivity.java
/**
* This shows how to create a simple activity with a map and a marker on the map.
* <p>
* Notice how we deal with the possibility that the Google Play services APK is not
* installed/enabled/updated on a user's device.
*/
public class BasicMapActivity extends android.support.v4.app.FragmentActivity {
/**
* Note that this may be null if the Google Play services APK is not available.
*/
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setUpMapIfNeeded();
}
@Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}
/**
* Sets up the map if it is possible to do so (i.e., the Google Play services APK is correctly
* installed) and the map has not already been instantiated.. This will ensure that we only ever
* call {@link #setUpMap()} once when {@link #mMap} is not null.
* <p>
* If it isn't installed {@link SupportMapFragment} (and
* {@link com.google.android.gms.maps.MapView
* MapView}) will show a prompt for the user to install/update the Google Play services APK on
* their device.
* <p>
* A user can return to this Activity after following the prompt and correctly
* installing/updating/enabling the Google Play services. Since the Activity may not have been
* completely destroyed during this process (it is likely that it would only be stopped or
* paused), {@link #onCreate(Bundle)} may not be called again so we should call this method in
* {@link #onResume()} to guarantee that it will be called.
*/
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMap();
}
}
}
/**
* This is where we can add markers or lines, add listeners or move the camera. In this case, we
* just add a marker near Africa.
* <p>
* This should only be called once and when we are sure that {@link #mMap} is not null.
*/
private void setUpMap() {
mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
}
}
2. Display MapView in (Support)Fragment.
main.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- This can go anywhere in your layout. -->
<com.google.android.gms.maps.MapView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
MainActivity.java
/**
* This shows how to create a simple activity with a raw MapView and add a marker to it. This
* requires forwarding all the important lifecycle methods onto MapView.
*/
public class RawMapViewDemoActivity extends android.support.v4.app.FragmentActivity {
private MapView mMapView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mMapView = (MapView) findViewById(R.id.map);
mMapView.onCreate(savedInstanceState);
}
@Override
protected void onResume() {
super.onResume();
mMapView.onResume();
}
@Override
protected void onPause() {
mMapView.onPause();
super.onPause();
}
@Override
protected void onDestroy() {
mMapView.onDestroy();
super.onDestroy();
}
@Override
public void onLowMemory() {
super.onLowMemory();
mMapView.onLowMemory();
}
}