I want to show an alertbox in my MapsActivity, if I enter a geofence. I can detect the "enter", and i can also trigger a notification, but when i want to create an alert box I get this error:
01-15 20:10:08.443 17477-17797/com.example.labsw.bugavo E/AndroidRuntime: FATAL EXCEPTION: IntentService[GeofenceReceiver]
Process: com.example.labsw.bugavo, PID: 17477
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:107)
at com.example.labsw.bugavo.navigation.MapsActivity.triggerAlertBoxOnFenceEntry(MapsActivity.java:669)
at com.example.labsw.bugavo.navigation.geofencing.GeofenceReceiver.onHandleIntent(GeofenceReceiver.java:95)
at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:66)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.os.HandlerThread.run(HandlerThread.java:61)
It seems like i dont get the right context, but i tried
AlertDialog.Builder alertbox = new AlertDialog.Builder(getApplicationContext());
and
AlertDialog.Builder alertbox = new AlertDialog.Builder(getBaseContext);
also, but nothing works.
Where I detect the "enter" and trigger the method for the alertbox (GeofenceReciever.java):
public GeofenceReceiver() {
super("GeofenceReceiver");
}
MapsActivity mapsActivity = new MapsActivity();
@Override
protected void onHandleIntent(Intent intent) {
Log.i(TAG, "onHandleIntent: GeofenceReceiver called");
GeofencingEvent geoEvent = GeofencingEvent.fromIntent(intent);
if (geoEvent.hasError()) {
Log.i(TAG, "Error GeofenceReceiver.onHandleIntent");
} else {
Log.i(TAG, "GeofenceReceiver : Transition -> "
+ geoEvent.getGeofenceTransition());
int transitionType = geoEvent.getGeofenceTransition();
geofenceId = 0;
if (transitionType == Geofence.GEOFENCE_TRANSITION_ENTER) {
List<Geofence> triggerList = geoEvent.getTriggeringGeofences();
for (Geofence geofence : triggerList) {
Log.i(TAG, "onHandleIntent: geofence id = " + geofence.getRequestId());
CustomGeofence sg = GeofenceAndStationsStore.getInstance()
.getSimpleGeofences().get(geofence.getRequestId());
geofenceId = sg.getStationId();
String transitionName = "";
switch (transitionType) {
case Geofence.GEOFENCE_TRANSITION_ENTER:
transitionName = "enter";
break;
}
String date = DateFormat.format("yyyy-MM-dd hh:mm:ss",
new Date()).toString();
GeofenceNotification geofenceNotification = new GeofenceNotification(
this);
geofenceNotification
.displayNotification(sg, transitionType);
}
}
if (transitionType == Geofence.GEOFENCE_TRANSITION_ENTER) {
Intent newintent = new Intent(this, MapsActivity.class);
arrivedAtStation = true;
if(geofenceId != 0) {
newintent.putExtra("stationId", geofenceId);
} else {
Log.i(TAG, "onHandleIntent: irgendwas stimmt nicht.. keine stationId von geofence vorhanden");
}
newintent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(newintent);
Log.i(TAG, "onHandleIntent: TRIGGER ALERT DIALOG");
mapsActivity.triggerAlertBoxOnFenceEntry();
}
}
}
}
and where i want to create the alertbox (MapsActivity.java):
public void triggerAlertBoxOnFenceEntry() {
AlertDialog.Builder alertbox = new AlertDialog.Builder(MapsActivity.this);
alertbox.setTitle("Station Erreicht!");
alertbox.setMessage("Sie haben die Station " + GeofenceReceiver.geofenceId + " erreicht! \nWollen Sie Die Fragerunde starten?\"");
alertbox.setPositiveButton("Ja", null);
alertbox.setNegativeButton("Nein", null);
alertbox.create();
alertbox.show();
System.out.println("Sie haben die Station " + GeofenceReceiver.geofenceId + " erreicht! \nWollen Sie Die Fragerunde starten?");
GeofenceReceiver.arrivedAtStation = false;
}
I also want to start a new Activity out of that alertbox (which doesn´t work either, i guess its because of the wrong context too) It would be awesome if you guys could help me out.