gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 13:06:56 27 4
gpt4 key购买 nike

我正在使用 Here map SDK,并已初始化 map 和 PositioningManager,以便在 initialize() 方法中获取用户的当前位置,现在我有 2 个 getDirections(View v) 这是从 View 布局xml文件调用的,因此它能够访问posManager,它是PositioningManager类的实例,但我的第二种方法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 View 的答案,但似乎都不起作用。如果我尝试从具有 View 引用的 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 的实例,即使它应该可以访问完成类,并且我可以在引用 View 的其他方法中访问它,即 getDirection(View v) 但是不在 track_my_ride() 方法

最佳答案

发生这种情况是因为您的初始 track_my_ride() 调用来自 onCreate()。很可能初始化尚未完成,并且因为 init 是异步的,所以您不能假设 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.
}
}

关于java - 如果不在函数中传递 View,则无法获取 PositioningManager 的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44112534/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com