无法将上下文传递给NotificationCompat.Builder

时间:2018-02-05 13:29:10

标签: android service android-notifications notificationmanager

我一直在尝试从后台服务构建通知,App在运行时崩溃,同时生成通知。这是我的日志

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)

这是我的服务类,它有sendNotification方法

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


}

我从广播接收器调用这个sendNotification方法,我已经尝试传递它自己的上下文但是构建一些如何生成nullpointerException。任何帮助将不胜感激

1 个答案:

答案 0 :(得分:1)

buildernull

据推测,您正在某处调用new AlarmService(),然后在sendNotification()实例上调用AlarmService。如果是这样,永远不会自己创建服务(或活动)的实例。 sendNotification()属于其他地方,因为AlarmService本身并未使用该方法。