gpt4 book ai didi

android - 显示通知后崩溃应用程序

转载 作者:行者123 更新时间:2023-11-29 17:54:19 25 4
gpt4 key购买 nike

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

    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 的手机上试了一下。有帮助吗?

最佳答案

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

您没有给您的通知一个 contentIntent,因为您使用的是 Compat 版本,您必须提供一个 Intent(当用户点击通知时打开的 Activity )或者如果您使用 RemoteViews,您可以使用应用于单个 View 的 PendingIntents .

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

关于android - 显示通知后崩溃应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20598200/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com