简体   繁体   English

单击导航抽屉中的项目后尝试启动地图活动

[英]Trying to initiate a Maps Activity after clicking a item in a Navigation Drawer

Recently I'm doing an app that have a Navigation Drawer and I have some items wich clik it should open a maps activity but when doing so my app stops and show me the following error:最近我正在做一个带有导航抽屉的应用程序,我有一些项目可以点击它应该打开一个地图活动,但是这样做时我的应用程序停止并显示以下错误:

FATAL EXCEPTION: main
Process: jocadoci.soloordena, PID: 2897 
java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0 
  at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255) 
  at java.util.ArrayList.get(ArrayList.java:308) 
  at jocadoci.soloordena.MapsItaly.geoLocate(MapsItaly.java:99) 
  at jocadoci.soloordena.MapsItaly.onMapReady(MapsItaly.java:73) 
  at com.google.android.gms.maps.SupportMapFragment$zza$1.zza(Unknown Source) 
  at com.google.android.gms.maps.internal.zzt$zza.onTransact(Unknown Source) 
  at android.os.Binder.transact(Binder.java:380) 
  at wl.a(:com.google.android.gms.DynamiteModulesB:82) 
  at maps.ad.t$5.run(Unknown Source) 
  at android.os.Handler.handleCallback(Handler.java:739) 
  at android.os.Handler.dispatchMessage(Handler.java:95) 
  at android.os.Looper.loop(Looper.java:135) 
  at android.app.ActivityThread.main(ActivityThread.java:5254) 
  at java.lang.reflect.Method.invoke(Native Method) 
  at java.lang.reflect.Method.invoke(Method.java:372) 
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)  

I supposed that the error is because in the maps activity I have the following class that use the ArrayList.java created by android studio.我认为该错误是因为在地图活动中我有以下使用由 android studio 创建的 ArrayList.java 的类。

private void geoLocate() throws IOException {
    String location = "comida rapida y pizza";
    Geocoder gc = new Geocoder(this);
    List<Address> list = new ArrayList<>(gc.getFromLocationName(location, 1));
    Address address = list.get(0);
    String locality = address.getLocality();

    Toast.makeText(this, locality, Toast.LENGTH_SHORT).show();

    double lat = address.getLatitude();
    double lng = address.getLongitude();
    goToLocationZoom(lat, lng, 16);

}

But I'm not sure what the error is.但我不确定错误是什么。 I appreciate any help and thanks in advance :)我感谢任何帮助并提前致谢:)

Geocoder.getFromLocationName will return null or empty list if no matches were found or there is no backend service available ( documentation ).如果未找到匹配项或没有可用的后端服务( 文档),则Geocoder.getFromLocationName将返回 null 或空列表。

So, to solve your problem you must check if there are available results:因此,要解决您的问题,您必须检查是否有可用结果:

private void geoLocate() throws IOException {
    String location = "comida rapida y pizza";
    Geocoder gc = new Geocoder(this);
    List<Address> list = new ArrayList<>(gc.getFromLocationName(location, 1));

    if (list != null && list.size() > 0) {
        Address address = list.get(0);
        String locality = address.getLocality();

        Toast.makeText(this, locality, Toast.LENGTH_SHORT).show();

        double lat = address.getLatitude();
        double lng = address.getLongitude();
        goToLocationZoom(lat, lng, 16);
    } else {
        Toast.makeText(this, "No results found", Toast.LENGTH_SHORT).show();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM