为什么onMapLongClick会抛出RuntimeException?

时间:2016-02-07 15:42:32

标签: java android nullpointerexception

我正在尝试将OnMapLongClickListener实施到我的地图应用程序中,这样每当用户长按屏幕时,标记就会出现在相应的位置。

我的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)

知道我做错了什么吗?我在Stack上看到了类似的答案,但似乎找不到解决我问题的方法。任何帮助将不胜感激。

感谢。

3 个答案:

答案 0 :(得分:0)

mMap.setOnMapLongClickListener(this);内拨打onMapReady()

mMapnull当您在其上调用它时,因为它之前的语句是异步语句。它不会等待你的回调onMapReady()完成或类似的事情。它在准备就绪时调用它。

答案 1 :(得分:0)

因为mMap null (尚未准备好)。

使用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);
    }

答案 2 :(得分: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);
    }
});

您可以详细了解Google地图和位置管理器 访问:http://www.studygtu.com/2016/02/google-map-implement-and-location.html