首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法在android中获得用户currentLocation

无法在android中获得用户currentLocation
EN

Stack Overflow用户
提问于 2015-11-07 18:16:49
回答 1查看 332关注 0票数 1

我将在地图中显示用户当前位置,但在onResume()(super.onResume())中有以下错误:

代码语言:javascript
复制
 @Override
    public void onResume() {
        **super.onResume();**
        mapView.onResume();
        mGoogleApiClient.connect();
    }
     java.lang.NullPointerException: Attempt to invoke interface method 'void com.google.maps.api.android.lib6.d.fk.o()' on a null object reference

我的课程扩展如下:

代码语言:javascript
复制
extends SupportMapFragment implements
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener,LocationListener

onCreateView:

代码语言:javascript
复制
mapView=(MapView)view.findViewById(R.id.map);
        mapView.onCreate(savedInstanceState);

        map = mapView.getMap();
        mGoogleApiClient = new GoogleApiClient.Builder(mActivity)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
        mLocationRequest = LocationRequest.create()
                .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
                .setInterval(10 * 1000)        // 10 seconds, in milliseconds
                .setFastestInterval(1 * 1000); // 1 second, in milliseconds

覆盖:

代码语言:javascript
复制
@Override
    public void onConnected(Bundle bundle) {
        Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
        if (location == null) {
            LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
        }
        else {
            handleNewLocation(location);
        };
    }

    @Override
    public void onConnectionSuspended(int i) {

    }


    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        if (connectionResult.hasResolution()) {
            try {
                // Start an Activity that tries to resolve the error
                connectionResult.startResolutionForResult(mActivity, CONNECTION_FAILURE_RESOLUTION_REQUEST);
            } catch (IntentSender.SendIntentException e) {
                e.printStackTrace();
            }
        } else {
            Log.i("TAG", "Location services connection failed with code " + connectionResult.getErrorCode());
        }
    }
    private void handleNewLocation(Location location) {
        double currentLatitude = location.getLatitude();
        double currentLongitude = location.getLongitude();
        LatLng latLng = new LatLng(currentLatitude, currentLongitude);
        MarkerOptions options = new MarkerOptions()
                .position(latLng)
                .title("I am here!");
        map.addMarker(options);
        map.moveCamera(CameraUpdateFactory.newLatLng(latLng));
    }
@Override
    public void onLocationChanged(Location location) {
        handleNewLocation(location);
    }

我真的不知道这个错误,请帮帮我。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-11-07 18:57:38

将此添加到onCreateView()

super.onCreateView(inflater, container, savedInstanceState);

没有它,你的onCreateView()是无用的。这就是为什么不调用您的mapView.onCreate(savedInstanceState);的原因。

请确保初始化mapView对象(new MapView),它可能为null。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33586137

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档