【发布时间】:2026-02-06 00:05:02
【问题描述】:
如果我输入地理围栏,我想在我的 MapsActivity 中显示一个警报框。我可以检测到“输入”,我也可以触发通知,但是当我想创建一个警报框时,我得到了这个错误:
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)
似乎我没有得到正确的上下文,但我试过了
AlertDialog.Builder alertbox = new AlertDialog.Builder(getApplicationContext());
和
AlertDialog.Builder alertbox = new AlertDialog.Builder(getBaseContext);
也可以,但没有任何效果。
我在哪里检测到“输入”并触发警报框的方法(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();
}
}
}
}
我想在哪里创建警报框(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;
}
我还想从那个警报框开始一个新的活动(这也不起作用,我猜它也是因为错误的上下文) 如果你们能帮助我,那就太棒了。
【问题讨论】:
-
不需要
triggerAlertBoxOnFenceEntry()方法。只需在 MapsActivity 的 onCreate() 中检查stationId额外,如果有stationId额外,则显示警报。
标签: java android nullpointerexception android-fragmentactivity android-context