尝试和捕捉错误的android错误、android

2023-09-06 06:50:06 作者:吃饭,睡觉,看EXO

我开发一个实现片段内地图Android应用程序。不过我与try和catch块的一个问题。我不断收到错误异常'com.google.android.gms.common.GooglePlayServicesNotAvailableException '是从来没有在相应的try块抛出。我怎样才能解决这个问题?先谢谢了。

下面是code:

 进口android.support.v4.app.Fragment;进口android.support.v7.app.AppCompatActivity;进口android.os.Bundle;进口android.view.LayoutInflater;进口android.view.Menu;进口android.view.MenuItem;进口android.view.View;进口android.view.ViewGroup;进口com.google.android.gms.common.GooglePlayServicesNotAvailableException;进口com.google.android.gms.maps.CameraUpdate;进口com.google.android.gms.maps.CameraUpdateFactory;进口com.google.android.gms.maps.GoogleMap;进口com.google.android.gms.maps.MapView;进口com.google.android.gms.maps.MapsInitializer;进口com.google.android.gms.maps.model.LatLng;公共类MapActivity扩展片段{    MapView类MapView类;    GoogleMap的地图;    @覆盖    公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){        视图V = inflater.inflate(R.layout.fragment_location,集装箱,FALSE);        //获取从XML布局的MapView和创建它        图形页面=(图形页面)v.findViewById(R.id.mapview);        mapView.onCreate(savedInstanceState);        //从获取的MapView到的GoogleMap并执行初始化的东西        地图= mapView.getMap();        map.getUiSettings()setMyLocationButtonEnabled(假)。        map.setMyLocationEnabled(真);        //需要做任何CameraUpdateFactory调用之前调用MapsInitializer        尝试{            MapsInitializer.initialize(this.getActivity());        }赶上(GooglePlayServicesNotAvailableException E){            e.printStackTrace();        }        //更新的MapView的位置和缩放        的CameraUpdate的CameraUpdate = CameraUpdateFactory.newLatLngZoom(新经纬度(43.1,-87.9),10);        map.animateCamera(的CameraUpdate);        返回伏;    }    @覆盖    公共无效onResume(){        mapView.onResume();        super.onResume();    }    @覆盖    公共无效的onDestroy(){        super.onDestroy();        mapView.onDestroy();    }    @覆盖    公共无效onLowMemory(){        super.onLowMemory();        mapView.onLowMemory();    }} 

中的XML文件:

 <?XML版本=1.0编码=UTF-8&GT?;< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android    机器人:layout_width =FILL_PARENT    机器人:layout_height =FILL_PARENT>    < com.google.android.gms.maps.MapView机器人:ID =@ + ID /图形页面        机器人:layout_width =FILL_PARENT        机器人:layout_height =FILL_PARENT/>< / LinearLayout中> 
Android真机调试连接服务器时出现问题怎么解决

解决方案

不要使用的try-catch。使用以下code处理错误:

 如果(MapsInitializer.initialize(活动)!= ConnectionResult.SUCCESS){    //这里做什么} 

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>

解决方案

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

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

 
精彩推荐
图片推荐