当用户进入地理围栏或居住在地理围栏中时,应用程序意外关闭



我已经在我的android应用程序中实现了Geofence服务,以便在用户进入、居住或退出Geofence时通知用户。在我的实现中,我可以通过长按特定位置在任何地方添加地理围栏。但当用户进入Geofence或添加Geofence时,用户已经在Geofence中,应用程序意外崩溃。

我该如何解决这个问题?

这是我的GeofenceBroadcastReceiver.java类。

public class GeoFenceBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = "GeoFenceBroadcastReceiv";
@Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
Toast.makeText(context, "Geofence triggered...", Toast.LENGTH_SHORT).show();
GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
if (geofencingEvent.hasError()) {
Log.d(TAG, "onReceive: Error receiving geofence event...");
return;
}
List<Geofence> geofenceList = geofencingEvent.getTriggeringGeofences();
for (Geofence geofence: geofenceList) {
Log.d(TAG, "onReceive: " + geofence.getRequestId());
}
Location location = geofencingEvent.getTriggeringLocation();
int transitionType = geofencingEvent.getGeofenceTransition();
switch (transitionType) {
case Geofence.GEOFENCE_TRANSITION_ENTER:
Toast.makeText(context, "GEOFENCE_TRANSITION_ENTER", Toast.LENGTH_SHORT).show();
break;
case Geofence.GEOFENCE_TRANSITION_DWELL:
Toast.makeText(context, "GEOFENCE_TRANSITION_DWELL", Toast.LENGTH_SHORT).show();
break;
case Geofence.GEOFENCE_TRANSITION_EXIT:
Toast.makeText(context, "GEOFENCE_TRANSITION_EXIT", Toast.LENGTH_SHORT).show();
break;
}
throw new UnsupportedOperationException("Not yet implemented");
}
}

谢谢!

在某些情况下,geofencingEvent返回null,因此可以实现相应的对策。