尝试在android中捕获错误

时间:2015-09-03 09:32:03

标签: java android google-maps try-catch

我正在开发一个在片段中实现Maps的Android应用程序。但是我遇到了try和catch块的问题。我一直收到错误Exception 'com.google.android.gms.common.GooglePlayServicesNotAvailableException'永远不会在相应的try块中抛出。我该如何解决这个错误?在此先感谢。

以下是代码:

 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();
    }

}

xml文件:

<?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>

4 个答案:

答案 0 :(得分:3)

不要使用try-catch。使用以下代码处理错误:

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

答案 1 :(得分:2)

自谷歌服务lib版本15以来,你试图捕获的异常不再被抛出。

有关解决方案,请参阅Google Maps Android API v2: GooglePlayServicesNotAvailableException not thrown anymore

链接问题的代码:

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

答案 2 :(得分:1)

如该链接底部所述; http://developer.android.com/google/play-services/setup.html

以及适当处理此示例的示例代码;

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();
}

请参阅此Google Maps Android API v2 throws GooglePlayServicesNotAvailableException, out of date, SupportMapFragment.getMap() returns null

答案 3 :(得分:1)

为什么不使用getMapAsync并监听onMapReady(GoogleMap map),如示例中所述:

@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);
}

然后在onMapReady中完成剩下的工作

    @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);
}

和相应的功能:你也可以输入zoomFactor

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

不需要初始化