发布Notification报错的问题

自己学习Notification的时候,写了一个练习,发布一个Notification。

但是在运行时候报了一个错误:

04-28 08:18:36.766: E/AndroidRuntime(683): FATAL EXCEPTION: main
04-28 08:18:36.766: E/AndroidRuntime(683): java.lang.IllegalArgumentException: contentView required: pkg=com.carter id=43 notification=Notification(vibrate=default,sound=default,defaults=0xffffffff,flags=0x0)
04-28 08:18:36.766: E/AndroidRuntime(683): at android.os.Parcel.readException(Parcel.java:1326)
04-28 08:18:36.766: E/AndroidRuntime(683): at android.os.Parcel.readException(Parcel.java:1276)
04-28 08:18:36.766: E/AndroidRuntime(683): at android.app.INotificationManager$Stub$Proxy.enqueueNotificationWithTag(INotificationManager.java:274)
04-28 08:18:36.766: E/AndroidRuntime(683): at android.app.NotificationManager.notify(NotificationManager.java:111)
04-28 08:18:36.766: E/AndroidRuntime(683): at android.app.NotificationManager.notify(NotificationManager.java:91)
04-28 08:18:36.766: E/AndroidRuntime(683): at com.carter.NotificationTestActivity$1.onClick(NotificationTestActivity.java:45)
04-28 08:18:36.766: E/AndroidRuntime(683): at android.view.View.performClick(View.java:2485)
04-28 08:18:36.766: E/AndroidRuntime(683): at android.view.View$PerformClick.run(View.java:9080)
04-28 08:18:36.766: E/AndroidRuntime(683): at android.os.Handler.handleCallback(Handler.java:587)
04-28 08:18:36.766: E/AndroidRuntime(683): at android.os.Handler.dispatchMessage(Handler.java:92)
04-28 08:18:36.766: E/AndroidRuntime(683): at android.os.Looper.loop(Looper.java:123)
04-28 08:18:36.766: E/AndroidRuntime(683): at android.app.ActivityThread.main(ActivityThread.java:3683)
04-28 08:18:36.766: E/AndroidRuntime(683): at java.lang.reflect.Method.invokeNative(Native Method)
04-28 08:18:36.766: E/AndroidRuntime(683): at java.lang.reflect.Method.invoke(Method.java:507)
04-28 08:18:36.766: E/AndroidRuntime(683): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-28 08:18:36.766: E/AndroidRuntime(683): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-28 08:18:36.766: E/AndroidRuntime(683): at dalvik.system.NativeStart.main(Native Method)

大致说的内容是,抛出了一个非法参数异常,原因是需要contentView


源代码是:

Intent intent = new Intent(NotificationTestActivity.this, OtherActivity.class);
				PendingIntent pendIntent = PendingIntent.getActivity(NotificationTestActivity.this, 0, intent, 0);
				
				
				Notification notification = new Notification();
				notification.icon = R.drawable.ok;
				notification.when = System.currentTimeMillis();
				notification.tickerText = "接收到了一条通知";
				notification.defaults = Notification.DEFAULT_ALL;
								
				NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
				manager.notify(NOTIFICATION_ID, notification);


分析了一下,找到了原因,原来是没有设置Notification.setLatestEventInfo()方法,

在notification各项属性之后,发送之前加上

notification.setLatestEventInfo(NotificationTestActivity.this, "点击查看", "点击查看详细内容", pendIntent);

后,一切正常了。

具体原因,还不知道,有朋友知道其中的原理还请多多指正。

你可能感兴趣的:(notification,android)