Android Facil
Android Facil

Reputation: 91

Error on use of setMyLocationEnabled

Help with my code, please, setMyLocationEnabled(true) gives an error.

The program closes when running, and I can not find the solution :(

MainActivity.java File:

public class MainActivity extends Activity  {

Button btnDireccion;
TextView lblDireccion;
public static double lat = 0;
public static double lon = 0;
public static boolean checked = false; //Se ha chequeado el mapa.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (android.os.Build.VERSION.SDK_INT > 9) {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }

    btnDireccion = (Button)findViewById(R.id.btnDireccion);
    lblDireccion = (TextView)findViewById(R.id.lblDireccion);
}

public void mostrarMapa(View view){
    Intent pantalla = new Intent(this, SeleccionarDireccion.class);
    startActivity(pantalla);
}}

SeleccionarDireccion.java

public class SeleccionarDireccion extends AppCompatActivity {
private LatLng latlong = new LatLng (-6.760644,-79.863413);
private GoogleMap map;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_seleccionar_direccion);
    MapFragment fMap = (MapFragment) getFragmentManager().findFragmentById(R.id.map);

    map.setMyLocationEnabled(true);
    map = fMap.getMap();

    map.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
        public void onMapLongClick(LatLng LL)  {
            MainActivity.lat = LL.latitude;
            MainActivity.lon = LL.longitude;
            regresar();

        }

    });

    MainActivity.checked = true;
    if (map!=null){
        iniciarGPS();
    }

}

private void iniciarGPS(){
    map.addMarker(new MarkerOptions().position(latlong)
            .title("Ubicacion actual"));
    map.moveCamera(CameraUpdateFactory.newLatLngZoom(latlong,15));
}

public void regresar() {
    Intent intent = new Intent(this, MainActivity.class);
    intent .setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
    startActivity(intent);
    this.finish();

} }

activity_seleccionar_direccion.xml

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map"
tools:context="com.androidmorefast.moden.appcapturardireccion.SeleccionarDireccion"
android:name="com.google.android.gms.maps.MapFragment" />

Activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.androidmorefast.moden.appcapturardireccion.MainActivity">

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:orientation="vertical" >

            <Button
                android:id="@+id/btnDireccion"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Dirección"
                android:onClick="mostrarMapa" />

            <TextView
                android:id="@+id/lblDireccion"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal"
                android:text=" "
                android:textAppearance="?android:attr/textAppearanceLarge" />


        </LinearLayout>

    </LinearLayout>

</RelativeLayout>

Upvotes: 0

Views: 213

Answers (1)

antonio
antonio

Reputation: 18252

The getMap method is deprecated and in your case is returning null, so the line map.setMyLocationEnabled(true); is throwing NullPointerException. You should use getMapAsync:

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // ...
        MapFragment fMap = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
        fmap.getMapAsync(this);
    }

     @Override
    public void onMapReady(final GoogleMap googleMap) {
        map = googleMap;

        map.setMyLocationEnabled(true);
        map.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
            public void onMapLongClick(LatLng LL)  {
                MainActivity.lat = LL.latitude;
                MainActivity.lon = LL.longitude;
                regresar();
            }
        });

        iniciarGPS();
    }
}

Upvotes: 1

Related Questions