如果不在函数中传递 View,则无法获取 PositioningManager 的实例

Unable to get instance of PositioningManager without passing View in the function

我正在使用 Here maps SDK 并初始化了地图和 PositioningManager 以在 initialize() 方法中获取用户的当前位置现在我有 2 个 getDirections(View v)view layout xml file 因此它能够访问 posManager 这是 PositioningManager class 的实例,但是我的第二个方法 track_my_rideButton.onclick 方法调用,当我尝试使用 posManager 时应用程序抛出错误

com.sjain.routeplanner E/AndroidRuntime: FATAL EXCEPTION: main Process: com.sjain.routeplanner, PID: 26948 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sjain.routeplanner/com.sjain.routeplanner.ViewMapActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'com.here.android.mpa.common.GeoPosition com.here.android.mpa.common.PositioningManager.getPosition()' on a null object reference

......

     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.here.android.mpa.common.GeoPosition
    com.here.android.mpa.common.PositioningManager.getPosition()' on a
     null object reference

at com.sjain.routeplanner.ViewMapActivity.track_my_ride(ViewMapActivity.java:334) at com.sjain.routeplanner.ViewMapActivity.intentInfo(ViewMapActivity.java:137) at com.sjain.routeplanner.ViewMapActivity.onCreate(ViewMapActivity.java:101) at android.app.Activity.performCreate(Activity.java:6298)

.....

我已经阅读了如何通过当前 activity 视图的答案,但其中 none 似乎有效。 如果我尝试从具有视图引用的 getDirection(View v) 方法访问 posManager,则不会发生此异常,因此我想这不是我的 posManager 未初始化的问题。

这是我的 ViewMapActivity.java 文件

public class ViewMapActivity extends AppCompatActivity {
    private static final String LOG_TAG = ViewMapActivity.class.getSimpleName();

    // permissions request code
    private final static int REQUEST_CODE_ASK_PERMISSIONS = 1;

    /**
     * Permissions that need to be explicitly requested from end user.
     */
    private static final String[] REQUIRED_SDK_PERMISSIONS = new String[]{
            Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.WRITE_EXTERNAL_STORAGE,
    Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.ACCESS_COARSE_LOCATION};

    // map embedded in the map fragment
    private Map map = null;

    // map fragment embedded in this activity
    private MapFragment mapFragment = null;

    // TextView for displaying the current map scheme
    private TextView textViewResult;

    // MapRoute for this activity
    private static MapRoute mapRoute = null;

    private PositioningManager posManager;
    boolean paused = false;
    private MapMarker marker;
    private RouteItem route = null;

    // Define positioning listener
    PositioningManager.OnPositionChangedListener positionListener = new
            PositioningManager.OnPositionChangedListener() {

                public void onPositionUpdated(PositioningManager.LocationMethod method,
                                              GeoPosition position, boolean isMapMatched) {
                    // set the center only when the app is in the foreground
                    // to reduce CPU consumption
                    if (!paused) {
                        map.setCenter(position.getCoordinate(),
                                Map.Animation.NONE);
                        Log.d("MainActivity.class", "onPositionUpdated: " + position.getCoordinate().toString());
                        marker.setCoordinate(position.getCoordinate());
                    }
                }

                @Override
                public void onPositionFixChanged(PositioningManager.LocationMethod locationMethod, PositioningManager.LocationStatus locationStatus) {
                    Log.d("MainActivity.class", "onPositionFixChanged: " + locationStatus);
                    String status = locationStatus.toString();
                    if (Objects.equals(status, "AVAILABLE")) {
                        marker.setCoordinate(posManager.getPosition().getCoordinate());
                    } else if (Objects.equals(status, "TEMPORARILY_UNAVAILABLE")) {
                        marker.setCoordinate(posManager.getLastKnownPosition().getCoordinate());
                    }
                }
            };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        checkPermissions();
        intentInfo();
    }

    private void intentInfo() {
        try{
            ......
            String type = this.getIntent().getStringExtra("type");
            // if type is display just display the route
            // if track then display the eta and current status
            if(Objects.equals(type, "track")) {
                String location = this.getIntent().getStringExtra("location");
                 track_my_ride(location);
            }
        }
        catch (Error e) {
            Log.e("ViewMapActivity.class", "intentInfo: ", e);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        paused = false;
        if (posManager != null) {
            posManager.start(PositioningManager.LocationMethod.GPS);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (posManager != null) {
            posManager.stop();
        }
        paused = true;
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        if (posManager != null) {
            // Cleanup
            posManager.removeListener(positionListener);
        }
        map = null;
        super.onDestroy();
    }

    private void initialize() {
        setContentView(R.layout.activity_map_view);

        // Search for the map fragment to finish setup by calling init().
        mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.mapFragment);
        mapFragment.init(new OnEngineInitListener() {
            @Override
            public void onEngineInitializationCompleted(OnEngineInitListener.Error error) {
                if (error == OnEngineInitListener.Error.NONE) {
                    // retrieve a reference of the map from the map fragment
                    map = mapFragment.getMap();
                    // Set the map center coordinate to the Vancouver region (no animation)
                    map.setCenter(new GeoCoordinate(19.152568, 72.855825), Map.Animation.NONE);
                    // Set the map zoom level to the average between min and max (no animation)
                    map.setZoomLevel((map.getMaxZoomLevel() + map.getMinZoomLevel()) / 2);

                    // get users current location
                    posManager = PositioningManager.getInstance();
                    posManager.start(PositioningManager.LocationMethod.GPS);
                    posManager.addListener(new WeakReference<>(positionListener));
                    // create marker to display users current location
                    marker = new MapMarker();
                    marker.setCoordinate(posManager.getLastKnownPosition().getCoordinate());
                    map.addMapObject(marker);
                } else {
                    Log.e(LOG_TAG, "Cannot initialize MapFragment (" + error + ")");
                }
            }
        });
    }

    /**
     * Checks the dynamically controlled permissions and requests missing permissions from end user.
     */
    protected void checkPermissions() {
        final List<String> missingPermissions = new ArrayList<>();
        // check all required dynamic permissions
        for (final String permission : REQUIRED_SDK_PERMISSIONS) {
            final int result = ContextCompat.checkSelfPermission(this, permission);
            if (result != PackageManager.PERMISSION_GRANTED) {
                missingPermissions.add(permission);
            }
        }
        if (!missingPermissions.isEmpty()) {
            // request all missing permissions
            final String[] permissions = missingPermissions
                    .toArray(new String[missingPermissions.size()]);
            ActivityCompat.requestPermissions(this, permissions, REQUEST_CODE_ASK_PERMISSIONS);
        } else {
            final int[] grantResults = new int[REQUIRED_SDK_PERMISSIONS.length];
            Arrays.fill(grantResults, PackageManager.PERMISSION_GRANTED);
            onRequestPermissionsResult(REQUEST_CODE_ASK_PERMISSIONS, REQUIRED_SDK_PERMISSIONS,
                    grantResults);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[],
                                           @NonNull int[] grantResults) {
        switch (requestCode) {
            case REQUEST_CODE_ASK_PERMISSIONS:
                for (int index = permissions.length - 1; index >= 0; --index) {
                    if (grantResults[index] != PackageManager.PERMISSION_GRANTED) {
                        // exit the app if one permission is not granted
                        Toast.makeText(this, "Required permission '" + permissions[index]
                                + "' not granted, exiting", Toast.LENGTH_LONG).show();
                        finish();
                        return;
                    }
                }
                // all permissions were granted
                initialize();
                break;
        }
    }
    public void getDirections(View v) {
    // posManager Works here without any error.
        GeoCoordinate coordinate;
        if(posManager.getPosition().getCoordinate() != null) {
            coordinate = posManager.getPosition().getCoordinate();
        }
        else if(posManager.getLastKnownPosition().getCoordinate() != null ){
            coordinate = posManager.getLastKnownPosition().getCoordinate();
        }
        else {
            // default nesco it park coordinates
            //coordinates taken from here maps
            coordinate = new GeoCoordinate(19.15254,72.85571);
        }
    }

    public void track_my_ride(String location) {
        // 1. clear previous results
        textViewResult = (TextView) findViewById(R.id.result);
        textViewResult.setText("");
        if (map != null && mapRoute != null) {
            map.removeMapObject(mapRoute);
            mapRoute = null;
        }

        // 2. Initialize RouteManager
        RouteManager routeManager = new RouteManager();

        // 3. Select routing options
        RoutePlan routePlan = new RoutePlan();

        RouteOptions routeOptions = new RouteOptions();
        routeOptions.setTransportMode(RouteOptions.TransportMode.CAR);
        routeOptions.setRouteType(RouteOptions.Type.FASTEST);
        routePlan.setRouteOptions(routeOptions);

        GeoCoordinate coordinate;
        if(posManager.getPosition().getCoordinate() != null) {
            coordinate = posManager.getPosition().getCoordinate();
        }
        else if(posManager.getLastKnownPosition().getCoordinate() != null ){
            coordinate = posManager.getLastKnownPosition().getCoordinate();
        }
        else {
            // default nesco it park coordinates
            //coordinates taken from here maps
            coordinate = new GeoCoordinate(19.15254,72.85571);
        }
        // 4. Select Waypoints for your routes
        routePlan.addWaypoint(coordinate);

        // supposed to be current or last known location of bus
        String[] bus_coordinate = location.split(",");
        routePlan.addWaypoint(new GeoCoordinate(
                Double.parseDouble(bus_coordinate[0]),
                Double.parseDouble(bus_coordinate[1])));

        // 5. Retrieve Routing information via RouteManagerEventListener
        RouteManager.Error error = routeManager.calculateRoute(routePlan, routeManagerListener);
        if (error != RouteManager.Error.NONE) {
            Toast.makeText(getApplicationContext(),
                    "Route calculation failed with: " + error.toString(), Toast.LENGTH_SHORT)
                    .show();
        }
    }

    private RouteManager.Listener routeManagerListener = new RouteManager.Listener() {
        public void onCalculateRouteFinished(RouteManager.Error errorCode,
                                             List<RouteResult> result) {

            if (errorCode == RouteManager.Error.NONE && result.get(0).getRoute() != null) {
                // create a map route object and place it on the map
                mapRoute = new MapRoute(result.get(0).getRoute());
                map.addMapObject(mapRoute);

                // Get the bounding box containing the route and zoom in (no animation)
                GeoBoundingBox gbb = result.get(0).getRoute().getBoundingBox();
                map.zoomTo(gbb, Map.Animation.NONE, Map.MOVE_PRESERVE_ORIENTATION);

                textViewResult.setText(String.format(getString(R.string.route_maneuvers), result.get(0).getRoute().getManeuvers().size()));
            } else {
                textViewResult.setText(
                        String.format("Route calculation failed: %s", errorCode.toString()));
            }
        }

        public void onProgress(int percentage) {
            textViewResult.setText(String.format(getString(R.string.precent_done), percentage));
        }
    };
}

有人可以帮我吗

编辑

看到错误后,我似乎没有初始化 posManager,但我在初始化 MAP 时已经初始化了它,没有任何错误。 我应该如何在 track_my_ride() 方法中获取 posManager 的实例,即使它应该可以访问以完成 class 并且我可以在其他引用 View 的方法中访问它,即 getDirection(View v)但不在 track_my_ride() 方法中

发生这种情况是因为您最初的 track_my_ride() 调用来自 onCreate()。很可能初始化尚未完成,并且由于初始化是异步的,您不能假设 HERE SDK 对象在 onCreate 点可用。如果您使用的是调试器,行为可能会有所不同,因为时间不同。

解决方法是在回调OnEngineInitListener#onEngineInitializationCompleted时调用intentInfo()

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    checkPermissions();
    intentInfo();   // NOT GOOD HERE.
}

private void intentInfo() {
    try{
        ......
        String type = this.getIntent().getStringExtra("type");
        // if type is display just display the route
        // if track then display the eta and current status
        if(Objects.equals(type, "track")) {
            String location = this.getIntent().getStringExtra("location");
             track_my_ride(location); // You're gonna NPE HERE.
        }
    }
    catch (Error e) {
        Log.e("ViewMapActivity.class", "intentInfo: ", e);  // You're gonna NPE catch  HERE.
    }
}