2016-06-18 81 views
0

請幫助我的代碼,setMyLocationEnabled(true)提供了一個錯誤。使用setMyLocationEnabled時出錯

程序運行時關閉,我不能找到解決辦法:(

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> 
+0

的可能的複製[?我如何使用谷歌地圖和的LocationManager顯示在Android棉花糖當前位置(http://stackoverflow.com/questions/34582370/how-can-i -use-google-maps-and-locationmanager-to-show-current-location-on-androi) –

+0

'setMyLocationEnabled(true)給出一個錯誤'你得到哪個錯誤? –

回答

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(); 
    } 
} 
+0

感謝所有人,我的解決方案是刪除此編碼:map.setMyLocationEnabled(true); –

+0

這比解決方案更適用於解決方案。我還注意到,在你的代碼中,你調用'map.setMyLocationEnabled(true);'在'map = fMap.getMap();'之前是錯誤的。無論如何,正如我在我的解決方案中所說的那樣,getMap()方法已被棄用。 – antonio