显示通知后崩溃应用程序

时间:2013-12-15 18:39:48

标签: android notifications

我想创建一个通知,但是在调用最后一行后应用程序崩溃了。

    NotificationCompat.Builder notification = 
            new NotificationCompat.Builder(this)
            .setContentTitle("My notification")
            .setContentText("Hello World!");

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

    mNotificationManager.notify(1, notification.build());

在LogCat中我发现了这个:

12-15 19:30:27.828: W/dalvikvm(22020): threadid=1: thread exiting with uncaught exception (group=0x40015560)
12-15 19:30:27.838: E/AndroidRuntime(22020): FATAL EXCEPTION: main
12-15 19:30:27.838: E/AndroidRuntime(22020): java.lang.IllegalArgumentException: contentIntent required: pkg=com.example.test id=1 notification=Notification(vibrate=null,sound=null,defaults=0x0,flags=0x0)
12-15 19:30:27.838: E/AndroidRuntime(22020):    at android.os.Parcel.readException(Parcel.java:1326)
12-15 19:30:27.838: E/AndroidRuntime(22020):    at android.os.Parcel.readException(Parcel.java:1276)
12-15 19:30:27.838: E/AndroidRuntime(22020):    at android.app.INotificationManager$Stub$Proxy.enqueueNotificationWithTag(INotificationManager.java:274)
12-15 19:30:27.838: E/AndroidRuntime(22020):    at android.app.NotificationManager.notify(NotificationManager.java:133)
12-15 19:30:27.838: E/AndroidRuntime(22020):    at android.app.NotificationManager.notify(NotificationManager.java:104)
12-15 19:30:27.838: E/AndroidRuntime(22020):    at com.example.test.MainActivity.onClick(MainActivity.java:55)
12-15 19:30:27.838: E/AndroidRuntime(22020):    at android.view.View.performClick(View.java:2506)
12-15 19:30:27.838: E/AndroidRuntime(22020):    at android.view.View$PerformClick.run(View.java:9112)
12-15 19:30:27.838: E/AndroidRuntime(22020):    at android.os.Handler.handleCallback(Handler.java:587)
12-15 19:30:27.838: E/AndroidRuntime(22020):    at android.os.Handler.dispatchMessage(Handler.java:92)
12-15 19:30:27.838: E/AndroidRuntime(22020):    at android.os.Looper.loop(Looper.java:130)
12-15 19:30:27.838: E/AndroidRuntime(22020):    at android.app.ActivityThread.main(ActivityThread.java:3835)
12-15 19:30:27.838: E/AndroidRuntime(22020):    at java.lang.reflect.Method.invokeNative(Native Method)
12-15 19:30:27.838: E/AndroidRuntime(22020):    at java.lang.reflect.Method.invoke(Method.java:507)
12-15 19:30:27.838: E/AndroidRuntime(22020):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
12-15 19:30:27.838: E/AndroidRuntime(22020):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
12-15 19:30:27.838: E/AndroidRuntime(22020):    at dalvik.system.NativeStart.main(Native Method)
12-15 19:35:27.888: I/Process(22020): Sending signal. PID: 22020 SIG: 9

我在Android 2.3.7版的手机上试用。有什么帮助吗?

2 个答案:

答案 0 :(得分:3)

java.lang.IllegalArgumentException: contentIntent required: pkg=com.example.test id=1 

您没有向contentIntent发送通知,因为您使用Compat版本,您必须提供Intent(当用户点击通知时要打开的活动)或者如果您使用RemoteView,您可以使用应用于各个视图的PendingIntents

请参阅:http://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#setContentIntent(android.app.PendingIntent)

答案 1 :(得分:0)

  

您应该将意图传递给通知。