Problème NullPointerException java

Fermé
swiftapp - 22 juil. 2020 à 16:00
 swiftapp - 23 juil. 2020 à 16:24
Bonjour, j'ai un problème de context qui revient souvent, et impossible de le résoudre.

J'ai une méthode qui appel une fonction qui envoie une notification:
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onSensorChanged(SensorEvent event) {
        final float x = event.values[0];
        final float y = event.values[1];
        final float z = event.values[2];

        Log.i("TAG", x + " / " + y + " / " + z);

        if ((z < -2.5f) || (z > 2.5f)){
            pasZ++;
        }
        else if ((z > -2.5f) && (z < 2.5f) && (pasZ > 1) && (pasZ < 15)){

            pasActuel++;

            showNotif(pasActuel, pasTotal);

            pasZ = 0;
        }
        else {
            pasZ = 0;
        }

    }


et voila la fonction "showNotif":
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private void showNotif(int pas, int pasTotal){

        Intent notifIntent = new Intent(ServicePodo.this, MainActivity.class);
        notifIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        PendingIntent contentIntent = PendingIntent.getActivity(ServicePodo.this, 10, notifIntent, 0);

        Notification.Builder builder = new Notification.Builder(ServicePodo.this);

        builder.setSmallIcon(R.drawable.ic_baseline_directions_walk);
        builder.setColor(Color.GREEN);
        builder.setContentTitle("Podometre service");
        builder.setContentText("Vous avez déja fait " + pas + " pas sur " + pasTotal);
        builder.setContentIntent(contentIntent);
        builder.setOngoing(true);

        Notification notification = builder.build();
        manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        manager.notify(NOTIF_ID, notification);

    }


et voila les logs que je reçois quand je lance mon appli:

2020-07-22 15:37:30.037 21070-21070/fr.swiftapp.santemanager E/AndroidRuntime: FATAL EXCEPTION: main
Process: fr.swiftapp.santemanager, PID: 21070
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
at android.content.ContextWrapper.getPackageName(ContextWrapper.java:132)
at android.content.ComponentName.<init>(ComponentName.java:128)
at android.content.Intent.<init>(Intent.java:4965)
at fr.swiftapp.santemanager.Service.ServicePodo.showNotif(ServicePodo.java:102)
at fr.swiftapp.santemanager.Service.ServicePodo.onSensorChanged(ServicePodo.java:85)
at android.hardware.SystemSensorManager$SensorEventQueue.dispatchSensorEvent(SystemSensorManager.java:701)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:323)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:6178)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:891)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:781)


Apparement c'est une erreur de context sur cette ligne:
Intent notifIntent = new Intent(ServicePodo.this, MainActivity.class);


Merci d’avance pour vos réponse !!!

Configuration: Linux / Chrome 84.0.4147.89
A voir également:

2 réponses

BunoCS Messages postés 15472 Date d'inscription lundi 11 juillet 2005 Statut Modérateur Dernière intervention 25 mars 2024 3 894
23 juil. 2020 à 09:27
Hello,
De quel type est ton object
ServicePodo
? De type
Service
?
Si oui, et en supposant que tu sois bien dans cette classe lors de l'appel, tu devrais juste avoir besoin de faire ceci :
Intent notifIntent = new Intent(this, MainActivity.class);

0