0

map から自分の場所を取得できません。null ポインター例外がスローされます。

        if (mMap == null) {
        // Try to obtain the map from the SupportMapFragment.
        try {
            mMap = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.mapFrag)).getMap();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
        // Check if we were successful in obtaining the map.
        if (mMap != null) {
            mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
            mMap.setMyLocationEnabled(true);
            mMap.getUiSettings().setMyLocationButtonEnabled(true);
            mMap.getUiSettings().setZoomControlsEnabled(true);
            putMarkerOnMap(mMap.getMyLocation().getLatitude(),mMap.getMyLocation().getLongitude());
        }

行でエラーがスローされます..関数 putMarkerOnMap(lat,lng) を呼び出している場所は、その関数です

    public void putMarkerOnMap(double la , double lo){
    LatLng position = new LatLng(la,lo);
    mMap.addMarker(new MarkerOptions().position(new LatLng(la, lo)).title("Your Location"));
    CameraPosition campos = new CameraPosition.Builder().target(position).zoom(15)
            .bearing(90)
            .tilt(40)
            .build();
    mMap.animateCamera(CameraUpdateFactory.newCameraPosition(campos));
}
4

2 に答える 2

1

この行でNPEが発生していると思います

mMap.getMyLocation().getLatitude()

また、現在の場所を取得したい場合は、Google Play Services や Android 組み込みライブラリを使用するなど、別のアプローチを使用する必要があります

Google Play サービス: http://developer.android.com/training/location/retrieve-current.html

Android 組み込みライブラリ: http://developer.android.com/guide/topics/location/strategies.html

于 2016-03-27T07:52:56.573 に答える
1

このメソッドgetMyLocation()は非推奨です。使用しないでください。代わりに、Google Play Services が提供する FusedLocation API を使用して、現在の場所を取得します。

これをチェック

于 2016-03-27T08:04:07.107 に答える