android - Current Location input in LatLng causing crash -


i'm trying put marker @ current location (using google maps api). while direct input values in latlng working, indirectly calling currentlocation.getlatitude() , currentlocation.getlongitude() crashing application reason, know it's line because app doesn't crash until add line.

here's mapsactivity class-

    package com.example.shreyass.tourist;  import android.manifest; import android.content.pm.packagemanager; import android.location.location; import android.location.locationmanager; import android.support.annotation.nonnull; import android.support.annotation.nullable; import android.support.v4.app.activitycompat; import android.support.v4.app.fragmentactivity; import android.os.bundle; import android.support.v4.content.contextcompat;  import com.google.android.gms.common.connectionresult; import com.google.android.gms.location.locationservices; import com.google.android.gms.maps.cameraupdatefactory; import com.google.android.gms.maps.googlemap; import com.google.android.gms.maps.onmapreadycallback; import com.google.android.gms.maps.supportmapfragment; import com.google.android.gms.maps.internal.igooglemapdelegate; import com.google.android.gms.maps.model.latlng; import com.google.android.gms.maps.model.markeroptions; import com.google.android.gms.common.api.googleapiclient;   public class mapsactivity extends fragmentactivity implements       onmapreadycallback,googleapiclient.connectioncallbacks,googleapiclient.onconnectionfailedlistener{  private googlemap mmap; private location currentlocation; googleapiclient googleapiclient;  @override protected void onstop() {     super.onstop();     googleapiclient.disconnect(); }  @override protected void oncreate(bundle savedinstancestate) {     super`enter code here`.oncreate(savedinstancestate);     setcontentview(r.layout.activity_maps);      if (googleapiclient == null) {         googleapiclient = new googleapiclient.builder(this)                 .addconnectioncallbacks(this)                 .addonconnectionfailedlistener(this)                 .addapi(locationservices.api)                 .build();     }      googleapiclient.connect();      // obtain supportmapfragment , notified when map ready used.     supportmapfragment mapfragment = (supportmapfragment) getsupportfragmentmanager()             .findfragmentbyid(r.id.map);     mapfragment.getmapasync(this);  }   /**  * manipulates map once available.  * callback triggered when map ready used.  * can add markers or lines, add listeners or move camera. in case,  * add marker near sydney, australia.  * if google play services not installed on device, user prompted install  * inside supportmapfragment. method triggered once user has  * installed google play services , returned app.  */ @override public void onmapready(googlemap googlemap) {      mmap = googlemap;      // add marker in sydney , move camera     latlng sydney = new latlng(currentlocation.getlatitude(), currentlocation.getlongitude());     mmap.addmarker(new markeroptions().position(sydney).title("marker @ current location"));     mmap.movecamera(cameraupdatefactory.newlatlng(sydney)); }  @override public void onconnected(@nullable bundle bundle) {      if(contextcompat.checkselfpermission(mapsactivity.this, manifest.permission.access_coarse_location)             !=packagemanager.permission_granted)     {         activitycompat.requestpermissions(mapsactivity.this,new string[]{manifest.permission.access_coarse_location},1);     }     else {         currentlocation = locationservices.fusedlocationapi.getlastlocation(googleapiclient);     }  }  @override public void onconnectionsuspended(int i) {  }  @override public void onconnectionfailed(@nonnull connectionresult connectionresult) {  } } 

try changes..

  public class mapsactivity extends fragmentactivity implements onmapreadycallback,googleapiclient.connectioncallbacks,googleapiclient.onconnectionfailedlistener{      private googlemap mmap;     private location currentlocation;     googleapiclient googleapiclient;      @override     protected void onstop() {         super.onstop();         googleapiclient.disconnect();     }      @override     protected void oncreate(bundle savedinstancestate) {         super`enter code here`.oncreate(savedinstancestate);         setcontentview(r.layout.activity_maps);            // obtain supportmapfragment , notified when map ready used.          supportmapfragment mapfragment = (supportmapfragment) getsupportfragmentmanager()                 .findfragmentbyid(r.id.map);         mapfragment.getmapasync(this);     }       /**      * manipulates map once available.       * callback triggered when map ready used.       * can add markers or lines, add listeners or move camera. in case,       * add marker near sydney, australia.       * if google play services not installed on device, user prompted install       * inside supportmapfragment. method triggered once user has       * installed google play services , returned app.       */     @override     public void onmapready(googlemap googlemap) {          mmap = googlemap;          if (googleapiclient == null) {             googleapiclient = new googleapiclient.builder(this)                     .addconnectioncallbacks(this)                     .addonconnectionfailedlistener(this)                     .addapi(locationservices.api)                     .build();         }          googleapiclient.connect();       }      @override     public void onconnected(@nullable bundle bundle) {          if(contextcompat.checkselfpermission(mapsactivity.this, manifest.permission.access_coarse_location)                 !=packagemanager.permission_granted)         {             activitycompat.requestpermissions(mapsactivity.this,new string[]{manifest.permission.access_coarse_location},1);         }         else {             currentlocation = locationservices.fusedlocationapi.getlastlocation(googleapiclient);              // add marker in sydney , move camera              latlng sydney = new latlng(currentlocation.getlatitude(), currentlocation.getlongitude());             mmap.addmarker(new markeroptions().position(sydney).title("marker @ current location"));             mmap.movecamera(cameraupdatefactory.newlatlng(sydney));         }      }      @override     public void onconnectionsuspended(int i) {      }      @override     public void onconnectionfailed(@nonnull connectionresult connectionresult) {      } } 

Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -