【发布时间】:2017-05-20 13:24:56
【问题描述】:
我是 Android 的初学者。我正在尝试在特定日期显示通知。当我尝试运行 showNotification() 方法时,出现以下错误:
java.lang.RuntimeException: 无法启动接收器 com.example.azernax.dforget.ScheduleNotification: java.lang.NullPointerException:尝试调用虚拟方法 'java.lang.String android.content.Context.getPackageName()' 为空 对象引用
我已尝试修复此错误,但没有成功。
ScheduleNotification:
public class ScheduleNotification extends WakefulBroadcastReceiver {
private AlarmManager alarmManager;
@Override
public void onReceive(Context context, Intent intent) {
ScheduleNotification.completeWakefulIntent(intent);
//create notification to show !!!
Toast.makeText(context, "TEST schedule!!!! ", Toast.LENGTH_LONG).show();
Notification_center notification = new Notification_center();
notification.showNotification(); //######################### CRASH !!!!
}
通知中心:
public class Notification_center extends AppCompatActivity {
public void showNotification()
{
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(R.drawable.icon);
builder.setContentTitle("dF Notification!");
builder.setContentText("description"); //--description event--
Intent intent = new Intent(this, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(intent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
NotificationManager NM = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NM.notify(0,builder.build());
}
【问题讨论】: