9it3e1
9it3e1

Reputation: 398

try and catch error in android

I am developing an android app that implements Maps inside a fragment. However I have a problem with the try and catch block. I keep getting the error Exception 'com.google.android.gms.common.GooglePlayServicesNotAvailableException' is never thrown in the corresponding try block. How can I solve this error? Thanks In Advance.

Here is the code:

 import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
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 com.google.android.gms.common.GooglePlayServicesNotAvailableException;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.MapsInitializer;
import com.google.android.gms.maps.model.LatLng;

public class MapActivity extends Fragment {

    MapView mapView;
    GoogleMap map;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_location, container, false);

        // Gets the MapView from the XML layout and creates it
        mapView = (MapView) v.findViewById(R.id.mapview);
        mapView.onCreate(savedInstanceState);

        // Gets to GoogleMap from the MapView and does initialization stuff
        map = mapView.getMap();
        map.getUiSettings().setMyLocationButtonEnabled(false);
        map.setMyLocationEnabled(true);

        // Needs to call MapsInitializer before doing any CameraUpdateFactory calls
        try  {
            MapsInitializer.initialize(this.getActivity());
        } catch (GooglePlayServicesNotAvailableException e) {
            e.printStackTrace();
        }

        // Updates the location and zoom of the MapView
        CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(43.1, -87.9), 10);
        map.animateCamera(cameraUpdate);

        return v;
    }

    @Override
    public void onResume() {
        mapView.onResume();
        super.onResume();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        mapView.onLowMemory();
    }

}

The xml file :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <com.google.android.gms.maps.MapView android:id="@+id/mapview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</LinearLayout>

Upvotes: 1

Views: 2895

Answers (4)

Malte
Malte

Reputation: 604

Why dont you use getMapAsync and listen for onMapReady(GoogleMap map) as described in example:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}

Then in onMapReady you do the rest

    @Override
public void onMapReady(GoogleMap map) {
    // Add a marker in Sydney, Australia, and move the camera.
    this.map = map;
    this.map.setOnMarkerClickListener(this);
    moveCamera(anyLatLngValue);
}

and the corresponding function: You could also enter the zoomFactor

    private void moveCamera(LatLng pos){
  map.moveCamera(CameraUpdateFactory.newLatLng(pos));
  map.moveCamera(CameraUpdateFactory.newCameraPosition(CameraPosition.fromLatLngZoom(pos, 15)));
}

No initialization needed there

Upvotes: 1

Sonu Kumar
Sonu Kumar

Reputation: 969

as described at the bottom of that link; http://developer.android.com/google/play-services/setup.html

and here comes sample code to handle this properly;

int checkGooglePlayServices =    GooglePlayServicesUtil.isGooglePlayServicesAvailable(mContext);
if (checkGooglePlayServices != ConnectionResult.SUCCESS) {
// google play services is missing!!!!
/* Returns status code indicating whether there was an error. 
Can be one of following in ConnectionResult: SUCCESS, SERVICE_MISSING, SERVICE_VERSION_UPDATE_REQUIRED, SERVICE_DISABLED, SERVICE_INVALID.
*/
   GooglePlayServicesUtil.getErrorDialog(checkGooglePlayServices, mActivity, 1122).show();
}

see this Google Maps Android API v2 throws GooglePlayServicesNotAvailableException, out of date, SupportMapFragment.getMap() returns null

Upvotes: 1

Ugurcan Yildirim
Ugurcan Yildirim

Reputation: 6132

Don't use try-catch. Handle the error using the following code:

if (MapsInitializer.initialize(activity) != ConnectionResult.SUCCESS) {
    // Do something here
}

Upvotes: 3

L.Butz
L.Butz

Reputation: 2616

The Exception you are trying to catch is not thrown anymore since google service lib version 15.

See Google Maps Android API v2: GooglePlayServicesNotAvailableException not thrown anymore for the solution.

Code from the linked question:

if (MapsInitializer.initialize(ctx) != ConnectionResult.SUCCESS) {
    // Handle the error
}

Upvotes: 2

Related Questions