Android:从服务发送通知时获取NullPointerException

时间:2014-01-03 13:36:05

标签: java android service notifications

我无法从我的Android应用服务发送通知,这是我的错误日志

01-03 20:41:01.075: W/dalvikvm(7009): threadid=11: thread exiting with uncaught exception (group=0x40fcd2a0)
01-03 20:26:31.795: E/AndroidRuntime(4186): FATAL EXCEPTION: Thread-1428
01-03 20:26:31.795: E/AndroidRuntime(4186): java.lang.NullPointerException
01-03 20:26:31.795: E/AndroidRuntime(4186):     at android.content.ComponentName.<init>(ComponentName.java:75)
01-03 20:26:31.795: E/AndroidRuntime(4186):     at android.content.Intent.<init>(Intent.java:3615)

这是我服务中的代码

int noid = 1;
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
    .setSmallIcon(R.drawable.ic_launcher)
    .setContentTitle("Title")
    .setContentText("Text message");
Intent resultIntent = new Intent(context, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
    stackBuilder.getPendingIntent(
        0,
    PendingIntent.FLAG_UPDATE_CURRENT
    );
mBuilder.setContentIntent(resultPendingIntent);

NotificationManager mNotificationManager =
    (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(noid, mBuilder.build());

请帮忙

谢谢

1 个答案:

答案 0 :(得分:2)

您传递给Context构造函数的Intentnull

ServiceContext所以在Service中你可以使用this作为上下文。