尝试捕获 android 中的错误

try and catch error in android

我正在开发一个 android 应用程序,它在片段内实现地图。但是我对 try and catch 块有疑问。我不断收到错误 Exception 'com.google.android.gms.common.GooglePlayServicesNotAvailableException' is never throw in the corresponding try block。我该如何解决这个错误?提前致谢。

代码如下:

 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>

自 google 服务库版本 15 以来,您尝试捕获的异常不再抛出。

请参阅 Google Maps Android API v2: GooglePlayServicesNotAvailableException not thrown anymore 了解解决方案。

来自链接问题的代码:

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

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

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

如底部所述link; 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

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

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

那里不需要初始化