【问题标题】:NullPointerException in Google Maps Fragmentactivity [duplicate]Google Maps Fragmentactivity 中的 NullPointerException [重复]
【发布时间】: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


【解决方案1】:

在 android 中不习惯实例化像 activity a = new Activity() 这样的活动。您应该为警报对话框创建一个静态方法,然后从您的接收器传递上下文,而不是尝试传递您使用默认构造函数创建的活动。

因为您是在接收器中执行此操作,所以最好使用通知而不是警报对话框。如果您需要该功能,您可以对通知执行操作。

【讨论】:

  • 首先感谢您的回复!
  • 我将 triggerAlertBoxOnFenceEntry 方法设为静态,并将 applicationContext 传递给它。我的应用程序现在没有崩溃,向前迈出了一大步,但现在我的地理围栏不再触发,我不知道为什么,因为它首先应该与它无关(比如触发动作),有什么建议吗? :) 哦,我不能使用通知,因为我们(我的团队和我)同意一个警报框,我不能改变它。
  • 丹尼尔斯的建议更好。如果可以,请使用上下文启动您的活动,然后在 onCreate() 中启动活动后,弹出警报对话框。类似于此处描述的内容:*.com/questions/3849868/…