Java Google地图碎片活动中的NullPointerException

Java Google地图碎片活动中的NullPointerException,java,android,nullpointerexception,android-fragmentactivity,android-context,Java,Android,Nullpointerexception,Android Fragmentactivity,Android Context,如果我输入地理围栏,我想在我的MapsActivity中显示一个alertbox。我可以检测“回车”,也可以触发通知,但当我想创建一个警报框时,会出现以下错误: 01-15 20:10:08.443 17477-17797/com.example.labsw.bugavo E/AndroidRuntime: FATAL EXCEPTION: IntentService[GeofenceReceiver]

如果我输入地理围栏,我想在我的MapsActivity中显示一个alertbox。我可以检测“回车”,也可以触发通知,但当我想创建一个警报框时,会出现以下错误:

    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());

还有,但什么都不管用

其中,我检测到“enter”并触发alertbox的方法(geofenceReceiver.java):

我还想从这个alertbox开始一个新的活动(这也不起作用,我猜也是因为上下文错误)
如果你们能帮我,那就太棒了

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


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

不需要使用
triggerAlertBoxOnFenceEntry()
方法。只需在MapsActivity的onCreate()中检查
stationId
extra,如果有
stationId
extra,则显示警报。首先感谢您的回复!我将triggerAlertBoxOnFenceEntry方法设置为静态,并将applicationContext传递给它。我的应用程序现在没有崩溃,这是向前迈出的一大步,但现在我的地理围栏不再触发了,我不知道为什么,因为它在一开始就应该与它无关(比如触发动作),有什么建议吗?:)哦,我不能使用通知,因为我们(我的团队和我)同意使用alertbox,我不能改变,丹尼尔斯的建议更好。如果可以,请使用上下文启动活动,然后在onCreate()中启动活动后,弹出警报对话框。类似于此处描述的内容:
     AlertDialog.Builder alertbox = new AlertDialog.Builder(getBaseContext);
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();
        }

    }
}
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;

}