发布Notification报错的问题

来源:互联网 发布:怎么看淘宝的图片大小 编辑:程序博客网 时间:2024/06/05 10:07

http://blog.csdn.net/carterjin/article/details/7520701

自己学习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=43notification=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


源代码是:

[java] view plaincopy
  1. Intent intent = new Intent(NotificationTestActivity.this, OtherActivity.class);  
  2.                 PendingIntent pendIntent = PendingIntent.getActivity(NotificationTestActivity.this0, intent, 0);  
  3.                   
  4.                   
  5.                 Notification notification = new Notification();  
  6.                 notification.icon = R.drawable.ok;  
  7.                 notification.when = System.currentTimeMillis();  
  8.                 notification.tickerText = "接收到了一条通知";  
  9.                 notification.defaults = Notification.DEFAULT_ALL;  
  10.                                   
  11.                 NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);  
  12.                 manager.notify(NOTIFICATION_ID, notification);  


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

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

[java] view plaincopy
  1. notification.setLatestEventInfo(NotificationTestActivity.this"点击查看""点击查看详细内容", pendIntent);  

后,一切正常了。
0 0
原创粉丝点击