2016-02-07 75 views
0

我試圖將OnMapLongClickListener應用到我的地圖應用程序,以便每當用戶長時間點擊屏幕時,相應位置就會出現一個標記。爲什麼onMapLongClick引發RuntimeException?

我的MainActivity類別如下:

import android.graphics.Color; 
import android.os.Bundle; 
import android.support.v4.app.FragmentActivity; 
import android.widget.Toast; 

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.model.BitmapDescriptorFactory; 
import com.google.android.gms.maps.model.CircleOptions; 
import com.google.android.gms.maps.model.LatLng; 
import com.google.android.gms.maps.model.MarkerOptions; 

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, GoogleMap.OnMapLongClickListener { 

    private GoogleMap mMap; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_maps); 
     // Obtain the SupportMapFragment and get notified when the map is ready to be used. 
     SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() 
       .findFragmentById(R.id.map); 
     mapFragment.getMapAsync(this); 
     mMap.setOnMapLongClickListener(this); 
    } 

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

     // Add a marker in Hatfield and move the camera 
     LatLng hatfield = new LatLng(51.7471060, -0.22978); 
     mMap.addMarker(new MarkerOptions() 
       .position(hatfield) 
       .title("Hertfordshire Times") 
       .snippet("Hertfordshire Lecturer wins Nobel Prize") 
       .draggable(true)); 
     mMap.moveCamera(CameraUpdateFactory.newLatLng(hatfield)); 

     mMap.addCircle(new CircleOptions() 
       .center(hatfield) 
       .fillColor(0x5500ff00) 
       .strokeColor(Color.WHITE).radius(60)); 
    } 

    @Override 
    public void onMapLongClick(final LatLng latLng) { 
     if(mMap != null) { 
      Toast.makeText(this, "Long press", Toast.LENGTH_LONG).show(); 
      mMap.addMarker(new MarkerOptions() 
      .position(latLng) 
      .title("You placed a marker here") 
      .icon(BitmapDescriptorFactory.defaultMarker())); 
     } 
    } 
} 

一旦OnMapLongClick方法運行時,它拋出一個NullPointerException(一個錯誤的片段如下所示):

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void  com.google.android.gms.maps.GoogleMap.setOnMapLongClickListener(com.google.android.gms.maps.GoogleMap$OnMapLongClickListener)' on a null object reference 
                          at com.example.paulmbw.map_application_fyp.MapsActivity.onCreate(MapsActivity.java:29) 

任何想法,我做錯了嗎?我在堆棧上看到了類似的答案,但似乎無法找到解決我的問題的方法。任何幫助將不勝感激。

謝謝。

+0

因爲'mMap'是** ** NULL – Rami

+0

將'mMap.setOnMapLongClickListener(this);'移動到'onMapReady()'方法。 –

+1

可能的重複[什麼是空指針異常,以及如何解決它?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do -i-fix-it) –

回答

0

撥打mMap.setOnMapLongClickListener(this);裏面onMapReady()

mMapnull當你調用一個就可以了,因爲權之前,它是一個異步一個聲明。它不會等待你的回調onMapReady()完成或類似的事情。它在準備好時調用它。

0

因爲mMapnull(尚未準備好)。

將您的聽衆移動到onMapReady()方法中。

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

     // Add a marker in Hatfield and move the camera 
     LatLng hatfield = new LatLng(51.7471060, -0.22978); 
     mMap.addMarker(new MarkerOptions() 
       .position(hatfield) 
       .title("Hertfordshire Times") 
       .snippet("Hertfordshire Lecturer wins Nobel Prize") 
       .draggable(true)); 
     mMap.moveCamera(CameraUpdateFactory.newLatLng(hatfield)); 

     mMap.addCircle(new CircleOptions() 
       .center(hatfield) 
       .fillColor(0x5500ff00) 
       .strokeColor(Color.WHITE).radius(60)); 

     mMap.setOnMapLongClickListener(MapsActivity.this); 
    } 
+1

謝謝你的回答。它的工作:) – archon101

0

執行在地圖上點擊

private GoogleMap map; 
private Marker marker; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
    Bundle savedInstanceState) { 
ActionBarActivity activity = (ActionBarActivity) getActivity(); 
activity.getSupportActionBar().setTitle(R.string.select_location); 
super.onCreateView(inflater, container, savedInstanceState); 
return inflater.inflate(R.layout.map, container, false); 
} 

@Override 
public void onViewCreated(View view, Bundle savedInstanceState) { 
super.onViewCreated(view, savedInstanceState); 
    Log.d("Map","On view created"); 
map = getMap(); 
     map.setMapType(GoogleMap.MAP_TYPE_SATELLITE); 
map.setOnMapClickListener(new OnMapClickListener() { 

    @Override 
    public void onMapClick(LatLng point) { 
     Log.d("Map","Map clicked"); 
     marker.remove(); 
     drawMarker(point); 
    } 
}); 

你可以看到更詳細的對於谷歌地圖和位置管理 訪問:http://www.studygtu.com/2016/02/google-map-implement-and-location.html

+0

這是一個**糟糕的主意,並可能無法解決問題 - 1)* getMap()*不保證返回一個非null對象。 - 2)* getMap()*已棄用。 – Rami

相關問題