Java android中的尝试捕捉错误
我正在开发一个android应用程序,在一个片段中实现地图。然而,我有一个问题,尝试和捕捉块。我不断收到错误Java android中的尝试捕捉错误,java,android,google-maps,try-catch,Java,Android,Google Maps,Try Catch,我正在开发一个android应用程序,在一个片段中实现地图。然而,我有一个问题,尝试和捕捉块。我不断收到错误Exception'com.google.android.gms.common.GooglePlayServicesNotAvailableException“从未在相应的try块中抛出。如何解决此错误?提前谢谢 代码如下: import android.support.v4.app.Fragment; import android.support.v7.app.AppCompatAct
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>
自从google service lib版本15以来,您尝试捕获的异常不再抛出 有关解决方案,请参阅 链接问题的代码:
if (MapsInitializer.initialize(ctx) != ConnectionResult.SUCCESS) {
// Handle the error
}
自google service lib版本15以来,您尝试捕获的异常不再抛出 有关解决方案,请参阅 链接问题的代码:
if (MapsInitializer.initialize(ctx) != ConnectionResult.SUCCESS) {
// Handle the error
}
不要使用try-catch。使用以下代码处理错误:
if (MapsInitializer.initialize(activity) != ConnectionResult.SUCCESS) {
// Do something here
}
不要使用try-catch。使用以下代码处理错误:
if (MapsInitializer.initialize(activity) != ConnectionResult.SUCCESS) {
// Do something here
}
如链接底部所述 下面是正确处理此问题的示例代码
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();
}
请参见该链接底部所述的 下面是正确处理此问题的示例代码
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();
}
请参见此为什么不使用getMapAsync并侦听onMapReady(谷歌地图),如示例中所述:
@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);
}
那么在一个月内你就可以做剩下的了
@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)));
}
那里不需要初始化为什么不使用getMapAsync并侦听onMapReady(谷歌地图),如示例中所述:
@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);
}
那么在一个月内你就可以做剩下的了
@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)));
}
那里不需要初始化您的意思是永远不会在相应的try块中抛出??您面临的错误是什么?您的意思是永远不会在相应的try块中抛出??你面临的错误是什么?