尝试并在android中捕获错误

我正在开发一个在片段中实现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文件:

     

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

请参阅Google Maps Android API v2:不再针对该解决方案抛出GooglePlayServicesNotAvailableException 。

来自链接问题的代码:

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

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

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

如该链接底部所述; 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(); } 

看到这个谷歌地图Android API v2抛出GooglePlayServicesNotAvailableException,过时,SupportMapFragment.getMap()返回null

为什么不使用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); } 

和相应的function:你也可以进入zoomFactor

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

那里不需要初始化