2016-02-07 76 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

相关问题