无法将上下文传递给 NotificationCompat.Builder
Unable to pass context to NotificationCompat.Builder
我一直在尝试从后台服务构建通知,应用程序在生成通知时在运行时崩溃。这是我的日志
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.run(AlarmReceiver.java:62)
at java.lang.Thread.run(Thread.java:818)
这是我的服务class,它有 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 的方法。任何帮助将不胜感激谢谢
builder
是 null
。
据推测,您正在某处调用 new AlarmService()
,然后在那个 AlarmService
实例上调用 sendNotification()
。如果是这样,永远不要自己创建服务实例(或activity)。 sendNotification()
属于其他地方,因为 AlarmService
本身没有使用该方法。
我一直在尝试从后台服务构建通知,应用程序在生成通知时在运行时崩溃。这是我的日志
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.run(AlarmReceiver.java:62)
at java.lang.Thread.run(Thread.java:818)
这是我的服务class,它有 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 的方法。任何帮助将不胜感激谢谢
builder
是 null
。
据推测,您正在某处调用 new AlarmService()
,然后在那个 AlarmService
实例上调用 sendNotification()
。如果是这样,永远不要自己创建服务实例(或activity)。 sendNotification()
属于其他地方,因为 AlarmService
本身没有使用该方法。