使用setMyLocationEnabled时出错

时间:2016-06-18 22:09:17

标签: android google-maps

请帮助我的代码,setMyLocationEnabled(true)会出错。

程序在运行时关闭,我找不到解决方案:(

MainActivity.java文件:

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>

1 个答案:

答案 0 :(得分:1)

getMap方法is deprecated并且在您的情况下返回null,因此行map.setMyLocationEnabled(true);正在投掷NullPointerException。您应该使用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();
    }
}