-2

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.

xBoLLo
  • 21
  • 9
  • 1
    There is no need for the `triggerAlertBoxOnFenceEntry()` method. Simply check for the `stationId` extra in onCreate() of MapsActivity, and if the `stationId` extra is there, show the alert. – Daniel Nugent Jan 15 '18 at 20:20

1 Answers1

0

It is not customary in android to instantiate activities like activity a = new Activity(). You should create a static method for your alert dialog and then pass in the context from your receiver instead of trying to pass in an activity you created using the default constructor.

Because you are doing this in a receiver it is probably best to use a notification instead of an alert dialog. You are able to put actions on notifications if you need that capability.

dazza5000
  • 5,339
  • 6
  • 34
  • 67
  • first of all thanks for your reply! – xBoLLo Jan 15 '18 at 20:59
  • I made the triggerAlertBoxOnFenceEntry method static, and passed the applicationContext to it. my app doesnt crash now, what is a big step forwards, but now my geofence doesnt trigger anymore, and i dont know why, because it should have nothing to do with it in the first place (like triggering the action), any suggestions?:) oh and i cant use notifications, because we (my team and i ) agreed on an alertbox, and i cant change that . – xBoLLo Jan 15 '18 at 21:05
  • Daniels suggestion is better. Start your activity using the context if you can and then once the activity is started in onCreate(), pop your alert dialog. Something similar to what is described here: https://stackoverflow.com/questions/3849868/startactivity-from-broadcastreceiver – dazza5000 Jan 16 '18 at 03:45