Search code examples
androidserviceandroid-notificationsnotificationmanager

Unable to pass context to NotificationCompat.Builder


I've been trying to build notification from background service, App crashes at runtime while generating notification. here is my log

E/AndroidRuntime: FATAL EXCEPTION: Thread-9364
                                                                                  Process: com.finepointmobile.roomtest1:remote, PID: 13623
                                                                                  java.lang.NullPointerException: Attempt to invoke virtual method 'android.support.v4.app.NotificationCompat$Builder android.support.v4.app.NotificationCompat$Builder.setSmallIcon(int)' on a null object reference
                                                                                      at com.jtstudiolab.gymmanagementsystem.AlarmService.sendNotification(AlarmService.java:62)
                                                                                      at com.jtstudiolab.gymmanagementsystem.AlarmReceiver$1.run(AlarmReceiver.java:62)
                                                                                      at java.lang.Thread.run(Thread.java:818)

Here is my service class which has sendNotification method

package ********;

import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.arch.persistence.room.Room;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import static android.app.PendingIntent.getBroadcast;

public class AlarmService extends Service {
    public Intent intent;
    public PendingIntent pendingIntent ;
    public NotificationCompat.Builder builder ;
    AlarmReceiver alarm ;
    Context c;
    public void onCreate()
    {
        c=this;
        alarm = new AlarmReceiver();
        intent = new Intent(c, MainActivity.class);
        pendingIntent = PendingIntent.getBroadcast(c, 0, intent, 0);
        builder = new NotificationCompat.Builder(getBaseContext(),"fck");
        Log.e("aaa","---------------service on create");
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId)
    {
        Log.e("aaa","---------------service on start command");
        Log.e("aaa","serviceis running");
        alarm.setAlarm(this);
        return START_STICKY;
    }

    @Override
    public void onStart(Intent intent, int startId)
    {
        Log.e("aaa","---------------sservice on ONSTART");
        alarm.setAlarm(this);
    }

    @Override
    public IBinder onBind(Intent intent)
    {
        return null;
    }
    public void sendNotification(String msg) {
// A pending Intent for reads

        builder.setSmallIcon(R.drawable.ic_menu_camera);
        // Set the intent that will fire when the user taps the notification.
        builder.setContentIntent(pendingIntent);

        builder.setAutoCancel(true);
        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_addperson));

        builder.setContentTitle("BasicNotifications Sample");
        builder.setContentText("Time to learn about notifications!");
        builder.setSubText("Tap to view documentation about notifications.");


        NotificationManager notificationManager = (NotificationManager) getSystemService(
                NOTIFICATION_SERVICE);
        notificationManager.notify(1, builder.build());
    }


}

I am calling this sendNotification method from broadcast receiver, I have tried passing its own context but build some how generates nullpointerException . Any help will be appreciated thanks


Solution

  • builder is null.

    Presumably, you are calling new AlarmService() somewhere, then are calling sendNotification() on that AlarmService instance. If so, never create an instance of a service (or activity) yourself. sendNotification() belongs somewhere else, as AlarmService is not using that method itself.