2014-03-13 68 views
0

我現在有與緯度,經度相關信息接收GCM消息的IntentService,我想註冊在後臺地理圍欄沒有前景的應用程序。我已經嘗試將工作(在一個活動中)代碼放入IntentService中,但是我得到了一個nullpointerexception,我不知道爲什麼。在後臺創建的Android地理圍欄沒有崩潰

@Override 
protected void onHandleIntent(Intent intent) { 
    Bundle extras = intent.getExtras(); 
    GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this); 
    String messageType = gcm.getMessageType(intent); 

    if (!extras.isEmpty()) { 
       Double lat = Double.parseDouble(extras.getString("lat")); 
       Double lng = Double.parseDouble(extras.getString("lng")); 
       Float radius = Float.parseFloat("600"); 
       createGeofences(lat,lng,radius); 

     } 
    } 
    GcmBroadcastReceiver.completeWakefulIntent(intent); 
} 

public void createGeofences(Double lat, Double lng, Float radius) { 


    mRequestType = GeofenceUtils.REQUEST_TYPE.ADD; 
    SimpleGeofence mGeofence = new SimpleGeofence(
     "1", 
     lat, 
     lng, 
     radius, 
     GEOFENCE_EXPIRATION_IN_MILLISECONDS, 
     Geofence.GEOFENCE_TRANSITION_ENTER); 

    // Store this flat version in SharedPreferences 
    //mPrefs.setGeofence("1", mGeofence); 


    /* 
    * Add Geofence objects to a List. toGeofence() 
    * creates a Location Services Geofence object from a 
    * flat object 
    */ 
    mCurrentGeofences.add(mGeofence.toGeofence()); 

    try { 
     mGeofenceRequester.addGeofences(mCurrentGeofences); 
    } catch (UnsupportedOperationException e) { 
     Toast.makeText(this, R.string.add_geofences_already_requested_error, 
        Toast.LENGTH_LONG).show(); 
    } 

這是錯誤消息:

java.lang.NullPointerException 
at com.app.portalalert.GcmIntentService.createGeofences(GcmIntentService.java:142) 
at com.app.portalalert.GcmIntentService.onHandleIntent(GcmIntentService.java:85) 
at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:136) 
at android.os.HandlerThread.run(HandlerThread.java:61) 

任何意見或建議,我怎麼能做到這一點?

+0

什麼在行'GcmIntentService.java:142'? – donfuxx

+0

mCurrentGeofences.add(mGeofence.toGeofence()); – Lorof

+0

你在哪裏初始化'mCurrentGeofences'?全球範圍內的 – donfuxx

回答

1

初始化mCurrentGeofences這樣的:

List<Geofence> mCurrentGeofences = new ArrayList<Geofence>(); 

而不只是:

List<Geofence> mCurrentGeofences; 
+0

我現在得到相同的錯誤,但用這一行:mGeofenceRequester.addGeofences(mCurrentGeofences); – Lorof

+0

你是如何初始化mGeofenceRequester的? – donfuxx

+0

私人GeofenceRequester mGeofenceRequester; – Lorof