我对地理围栏API在谷歌我发起/ O 2013 Creating and Monitoring Geofences的Android创建地理围栏
工作没有在上面的链接中给出的示例的方法如下:
public void createGeofences() {
........
mGeofenceList.add(mUIGeofence1.toGeofence());
mGeofenceList.add(mUIGeofence2.toGeofence());
}
这种方法在我的活动内部的onActivityResult中,并在同一活动中,我有按钮来保存地理栅栏并调用mLocationClient.connect() - >它与示例的addGeofences()方法相同。
@Override
private void onConnected(Bundle dataBundle) {
...
switch (mRequestType) {
case ADD :
// Get the PendingIntent for the request
mTransitionPendingIntent =
getTransitionPendingIntent();
// Send a request to add the current geofences
mLocationClient.addGeofences(
mGeofenceList, pendingIntent, this);
...
}
}
在这种方法中mGeofenceList为空,这不应该是因为它已经在onActivityResult方法之前更新。
问题:变量mGeofenceList在onActivityResult中更新,但在onConnected方法中访问时为空。我错在哪里?请帮忙。
我的第一个活动(A)是调用包含地图的活动B.在选择一个位置时,用户被带回到A,这就是调用onActivityResult的地方,并且确实收到了数据(我已经检查过Log)。但是,在onConnected(Bundle dataBundle)中检索时,变量(mGeofenceList)不包含更新的值。 现在,我已将数据返回保存在SharedPreferences中的onActivityResult中,并在需要onConnected时检索它,但仍然无法在此处找到丢失的概念。感谢您的回答:) – hshed
噢,好吧,这解释了很多:D以防万一:您确定您正确地引用您的全局列表吗?一旦花了我多年寻找类似的错误,只是为了找出我使用本地:ArrayList myList = new ArrayList();而不是全局的:myList = new ArrayList();我知道这很愚蠢,但是,有时候还是会发生的。 –
daemontus
是的,它是全球性的。它已经在fragment的onCreateView中实例化了 – hshed