Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
353 views
in Technique[技术] by (71.8m points)

Android app crashes when push notification is coming

I have a broadcast receiver for geofences. The exact same code works in another app where I have a whole Google Map in a Activity. In the new App I want the Google Map (fragment) as a part with some other buttons and text in one activity.

When I add this to Manifest.xml (see below), the app opens and at that moment when I get a Notification (I really get the notification I want) the app crashes.

<receiver
            android:name=".GeofenceBroadcastReceiver"
            android:enabled="true"
            android:exported="true"></receiver>

Without this <receiver ...> the app works but I dont get a notification.

So at some point this part is important but it might block something else?

Here is the GeofenceBroadcastReceiver.java: (at the end there is the switch/case where I call the Notification)

public class GeofenceBroadcastReceiver extends BroadcastReceiver {
    HauptActivity HA = new HauptActivity();

    private static final String TAG = "GeofenceBroadcastReceiv";

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO: This method is called when the BroadcastReceiver is receiving
        // an Intent broadcast.
        // Toast.makeText(context, "Geofence triggered...", Toast.LENGTH_SHORT).show();

        NotificationHelper notificationHelper = new NotificationHelper(context);

        GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);

        if (geofencingEvent.hasError()){
            Log.d(TAG, "onReceive: Error receiving geofence event...");
            return;
        }


        List<Geofence> geofenceList = geofencingEvent.getTriggeringGeofences();
        for (Geofence geofence: geofenceList) {
            Log.d(TAG, "onReceive: " + geofence.getRequestId());
        }
//        Location location = geofencingEvent.getTriggeringLocation();
        int transitionType = geofencingEvent.getGeofenceTransition();

        switch (transitionType) {
            case Geofence.GEOFENCE_TRANSITION_ENTER:
                Toast.makeText(context, "GEOFENCE_TRANSITION_ENTER", Toast.LENGTH_SHORT).show();
                notificationHelper.sendHighPriorityNotification("GEOFENCE_TRANSITION_ENTER", "", HauptActivity.class);
                HA.changeMeldungGefahr();
                break;
            case Geofence.GEOFENCE_TRANSITION_DWELL:
                Toast.makeText(context, "GEOFENCE_TRANSITION_DWELL", Toast.LENGTH_SHORT).show();
                notificationHelper.sendHighPriorityNotification("GEOFENCE_TRANSITION_DWELL", "", HauptActivity.class);
                break;
            case Geofence.GEOFENCE_TRANSITION_EXIT:
                Toast.makeText(context, "GEOFENCE_TRANSITION_EXIT", Toast.LENGTH_SHORT).show();
                notificationHelper.sendHighPriorityNotification("GEOFENCE_TRANSITION_EXIT", "", HauptActivity.class);
                break;
        }


    }
}

Here is the NotificationHelper.java (I got the code from the internet)

public class NotificationHelper extends ContextWrapper {

    private static final String TAG = "NotificationHelper";

    public NotificationHelper(Context base) {
        super(base);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            createChannels();
        }
    }

    private String CHANNEL_NAME = "High priority channel";
    private String CHANNEL_ID = "com.example.notifications" + CHANNEL_NAME;

    @RequiresApi(api = Build.VERSION_CODES.O)
    private void createChannels() {
        NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
        notificationChannel.enableLights(true);
        notificationChannel.enableVibration(true);
        notificationChannel.setDescription("this is the description of the channel.");
        notificationChannel.setLightColor(Color.RED);
        notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        manager.createNotificationChannel(notificationChannel);
    }

    public void sendHighPriorityNotification(String title, String body, Class activityname) { 

        Intent intent = new Intent(this, activityname);                         
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 267, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
//                .setContentTitle(title)
//                .setContentText(body)
                .setSmallIcon(R.drawable.ic_launcher_background)
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setStyle(new NotificationCompat.BigTextStyle().setSummaryText("überwachungskamera").setBigContentTitle(title).bigText(body))
                .setContentIntent(pendingIntent)
                .setAutoCancel(true)
                .build();

        NotificationManagerCompat.from(this).notify(new Random().nextInt(), notification);


    }

}

It is liturally the same code as I used before in the older app, and there everything worked fine. So I dont really want to change the code much because it should go perfectly. Maybe I forgot something somewhere else? Maybe the problem is not the code... but I didnt found the solution, maybe you have one.

EDIT: Here is the red text under "Run"

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.mapbox1, PID: 32082
    java.lang.RuntimeException: Unable to start receiver com.example.mapbox1.GeofenceBroadcastReceiver: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
        at android.app.ActivityThread.handleReceiver(ActivityThread.java:3399)
        at android.app.ActivityThread.-wrap18(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1780)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6944)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
        at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:163)
        at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:157)
        at android.content.Context.obtainStyledAttributes(Context.java:655)
        at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:839)
        at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:806)
        at androidx.appcompat.app.AppCompatDelegateImpl.findViewById(AppCompatDelegateImpl.java:630)
        at androidx.appcompat.app.AppCompatActivity.findViewById(AppCompatActivity.java:223)
        at com.example.mapbox1.HauptActivity.changeMeldungGefahr(HauptActivity.java:221)
        at com.example.mapbox1.GeofenceBroadcastReceiver.onReceive(GeofenceBroadcastReceiver.java:47)
        at android.app.ActivityThread.handleReceiver(ActivityThread.java:3392)
        at android.app.ActivityThread.-wrap18(Unknown Source:0)?
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1780)?
        at android.os.Handler.dispatchMessage(Handler.java:105)?
        at android.os.Looper.loop(Looper.java:164)?
        at android.app.ActivityThread.main(ActivityThread.java:6944)?
        at java.lang.reflect.Method.invoke(Native Method)?
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)?
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)?

And to make it clear: the app starts, I see the "HauptActivity", then I get a notification as required. But after the notification the app immediatly crashes.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

From the logs: calling HauptActivity.changeMeldungGefahr(HauptActivity.java:221) leads to a null pointer.

You should fix the Nullpointer within changeMeldungGefahr().


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...