7
I would like to know how to make the map show the current location of the user.
I created this code that apparently was working, only sometimes it fails to catch the location and application of the error.
public class VisualizarMapa extends FragmentActivity {
protected GoogleMap map;
@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.visualizarmapa);
getActionBar().setTitle("Visualizar Mapa");
getActionBar().setIcon(new ColorDrawable(getResources().getColor(android.R.color.transparent)));
SetupMapNull();
}
private void SetupMapNull(){
if (map == null){
map = ((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
if(map != null){
setUpMap();
}
}
}
private void setUpMap() {
map.setMyLocationEnabled(true);
LocationManager locationmanager = (LocationManager)getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationmanager.getBestProvider(criteria, true);
Location localatual = locationmanager.getLastKnownLocation(provider);
map.setMapType(map.MAP_TYPE_HYBRID);
double lat = localatual.getLatitude();
double longi = localatual.getLongitude();
LatLng LatLong = new LatLng(lat, longi);
map.moveCamera(CameraUpdateFactory.newLatLng(LatLong));
map.animateCamera(CameraUpdateFactory.zoomTo(19));
}
}
Logcat:
02-02 23:13:37.267: E/AndroidRuntime(9556): Process: br.find.me, PID: 9556
02-02 23:13:37.267: E/AndroidRuntime(9556): java.lang.RuntimeException: Unable to start activity ComponentInfo{br.find.me/br.find.me.VisualizarMapa}: java.lang.NullPointerException
02-02 23:13:37.267: E/AndroidRuntime(9556): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2202)
02-02 23:13:37.267: E/AndroidRuntime(9556): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2252)
02-02 23:13:37.267: E/AndroidRuntime(9556): at android.app.ActivityThread.access$800(ActivityThread.java:139)
02-02 23:13:37.267: E/AndroidRuntime(9556): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1200)
02-02 23:13:37.267: E/AndroidRuntime(9556): at android.os.Handler.dispatchMessage(Handler.java:102)
02-02 23:13:37.267: E/AndroidRuntime(9556): at android.os.Looper.loop(Looper.java:136)
02-02 23:13:37.267: E/AndroidRuntime(9556): at android.app.ActivityThread.main(ActivityThread.java:5103)
02-02 23:13:37.267: E/AndroidRuntime(9556): at java.lang.reflect.Method.invokeNative(Native Method)
02-02 23:13:37.267: E/AndroidRuntime(9556): at java.lang.reflect.Method.invoke(Method.java:515)
02-02 23:13:37.267: E/AndroidRuntime(9556): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
02-02 23:13:37.267: E/AndroidRuntime(9556): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606)
02-02 23:13:37.267: E/AndroidRuntime(9556): at dalvik.system.NativeStart.main(Native Method)
02-02 23:13:37.267: E/AndroidRuntime(9556): Caused by: java.lang.NullPointerException
02-02 23:13:37.267: E/AndroidRuntime(9556): at br.find.me.VisualizarMapa.setUpMap(VisualizarMapa.java:70)
02-02 23:13:37.267: E/AndroidRuntime(9556): at br.find.me.VisualizarMapa.SetupMapNull(VisualizarMapa.java:49)
02-02 23:13:37.267: E/AndroidRuntime(9556): at br.find.me.VisualizarMapa.onCreate(VisualizarMapa.java:36)
02-02 23:13:37.267: E/AndroidRuntime(9556): at android.app.Activity.performCreate(Activity.java:5275)
02-02 23:13:37.267: E/AndroidRuntime(9556): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
02-02 23:13:37.267: E/AndroidRuntime(9556): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2166)
And what’s the mistake? Edith your question including that information.
– Renan Gomes
I just edited!!!
– Mantir