Android -Java.lang.IllegalArgumentException:contentintent所需的错误是由通知引起的吗?

StackOverflow https://stackoverflow.com/questions/3112008

我有一项运行的服务,该服务在收到一条消息时会在通知栏中更新通知。

但是,当要更新通知时,我有时会遇到以下错误

java.lang.IllegalArgumentException: contentIntent required

这是我的代码:

可变设置


int icon = R.drawable.notification;
CharSequence tickerText = "Test";
long when = System.currentTimeMillis();
PendingIntent contentIntent;

Notification notification = new Notification(icon, tickerText, when);

NotificationManager mNotificationManager;

NotificationManager创建


    String ns = Context.NOTIFICATION_SERVICE;
    mNotificationManager = (NotificationManager) getSystemService(ns);

通知创建


    Intent notificationIntent = new Intent(this, TestsApp.class);
    contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
    notification.flags |= Notification.FLAG_NO_CLEAR;
    notification.icon = R.drawable.notification3;
    notification.setLatestEventInfo(this, "Registering", "Test", contentIntent);
    mNotificationManager.notify(1, notification);

通知的更新


    notification.icon = R.drawable.notification2;
    notification.setLatestEventInfo(getApplicationContext(), "Registered", "Test", contentIntent);
    mNotificationManager.notify(1, notification);   

因此,我的contentintent在线的某个地方发生了什么事,这是正确的吗?

它在我的服务类的顶部被声明为成员变量,除了上面显示的代码外,没有其他任何地方使用,因此在哪里可以重置为null?

有帮助吗?

解决方案

您需要为您的通知设置ContentIntent。

在您的情况下:

notification.contentIntent = notificationIntent;

否则,您将收到一条消息,即通知的内容符号为null,因为它没有设置。

该文档在这里: http://developer.android.com/reference/android/app/notification.html#contentintent

我在这里有一个例子: http://united-coders.com/nico-heid/show-progressbar-in-notification-inea-like-google-does-does-does-does-does-does-do and-downloading-from-android

其他提示

我认为这是因为Android OS版本

版本 2.3或更低,必须设置 contentintent,如果没有,您将获得此例外。

在我的项目中,我这样写了:

if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB) { Intent intent = new Intent(); PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, 0); mNotification.contentIntent = contentIntent; }

也许这可以帮助您!

就你而言

contentIntent = pendingIntent.getActivity(this,0,notificationIntent,0);

如果您想使用相同动作但其他额外的意图:

1)更改

requestCode

从默认的“ 0” in

getActivity (Context context, int requestCode, Intent intent, int flags)

对于``独特''

(int) System.currentTimeMillis();

` 2)

notification.contentintent = NotificationIntent;

这两个步骤都是强制性的,因为:

  • 如果没有选项1,选项2将无法正常工作。
  • 选项1将在没有2的情况下投掷非法分解。

就我而言,我有一个示例代码,并有一个通知要创建,而且我也收到了“ ContentIntents必需”错误 - Google将我带到了这个线程:D

这个问题的来源是我从示例代码中复制并将其粘贴到Eclipse项目中的引文。当我删除“”并将其键入后,问题解决了。也许这对某人有帮助。

这些是错误来源:nb.setContentTitle(“我的第一个通知!”); nb.setContentText(“ Hello”);

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top