Android - java.lang.IllegalArgumentException:由通知引起的 contentIntent 必需错误?
我正在运行一项服务,当它收到一条表示必须更改的消息时,它会更新通知栏中的通知。
但是,当要更新通知时,有时会出现以下错误
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沿线某处发生了一些事情,这是正确的吗?
它在我的 Service 类的顶部声明为成员变量,除了上面显示的内容之外,在代码中的其他任何地方都没有使用它,那么它在哪里可以重置为 null 呢?
I have a service running that updates a notification in the notification bar when it recieves a message saying it has to be changed.
However I get the following error sometimes when the notification is to be updated
java.lang.IllegalArgumentException: contentIntent required
Here is my code:
Variable setup
int icon = R.drawable.notification;
CharSequence tickerText = "Test";
long when = System.currentTimeMillis();
PendingIntent contentIntent;
Notification notification = new Notification(icon, tickerText, when);
NotificationManager mNotificationManager;
NotificationManager Creation
String ns = Context.NOTIFICATION_SERVICE;
mNotificationManager = (NotificationManager) getSystemService(ns);
Notification Creation
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);
Update of Notification
notification.icon = R.drawable.notification2;
notification.setLatestEventInfo(getApplicationContext(), "Registered", "Test", contentIntent);
mNotificationManager.notify(1, notification);
So something is happening my contentIntent somewhere along the line, would that be correct?
It is declared at the top of my Service class as a member variable and is not used anywhere else in the code apart from shown above, so where could it be getting reset to null?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为这是因为Android操作系统版本
2.3或更低版本,必须设置contentIntent,如果没有,你会得到这个异常。
在我的项目中,我这样写:
也许这可以帮助你!
I think this is because the Android OS Version
The version 2.3 or lower,must set contentIntent,if not,you will get this Exception.
In my project,I write like this:
Perhaps this could help you!
在您的情况下,
如果您想使用具有相同操作但不同附加功能的意图:
将
requestCode
from default"0"
in独特的内容,例如 这
两个步骤都是强制性的,因为:
In your case
if you want to use Intents with the same action but different extras:
Change
requestCode
from default"0"
into something unique like
Both steps are mandatory because:
就我而言,我有一个示例代码要做,需要创建一个通知,而且我还收到了“contentIntent required”错误 - 谷歌将我带到了这个线程:D
这个问题的根源是我从示例中复制的引用代码并将其粘贴到 eclipse 项目中。当我删除“”并重新输入它们时,问题就解决了。也许这对某人有帮助。
这些是错误的引用来源:
nb.setContentTitle("我的第一个通知!");
nb.setContentText("你好");
In my case, I had an example code to do, with a single Notification to create, and I also got "contentIntent required" error - google brought me to this thread :D
the source of this problem were quotations that I copied from an example code and pasted it in eclipse project. When I deleted " " and typed them back and problem was solved. Maybe this helps someone.
These were quotations source of error:
nb.setContentTitle("My first notification!");
nb.setContentText("Hello");
您需要为通知设置 contentIntent。
在您的情况下:
否则您将收到消息,通知的 contentIntent 为 null,因为它尚未设置。
该文档在这里: http://developer.android.com/reference/ android/app/Notification.html#contentIntent
我这里有一个小例子:http://united-coders.com/nico-heid/show-progressbar-in-notification-area-like-google-does-when -从android下载
you need to set the contentIntent for your notification.
in your case:
otherwise you will get the message, that the contentIntent of the notification is null, because it's not set.
the docu is here: http://developer.android.com/reference/android/app/Notification.html#contentIntent
i have a little example here: http://united-coders.com/nico-heid/show-progressbar-in-notification-area-like-google-does-when-downloading-from-android