通知中的IllegalArgumentException

时间:2012-11-08 06:22:35

标签: android notifications

我正在使用通知。

代码在使用API​​ 15的模拟器中完美地工作。但我正在使用API​​ 10强制关闭模拟器。

我收到了例外java.lang.IllegalArgumentException: contentIntent required:

我在清单中写了以下内容

<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15"/>

代码如下:

void showNotification(String status) {
    NotificationManager notificationManager;
    notificationManager = (NotificationManager) 
            getSystemService(Context.NOTIFICATION_SERVICE);
    int icon;
    if (status.equalsIgnoreCase("ON")) icon = R.drawable.on;
    else icon = R.drawable.off;
    String tickerText = "Phone Tracker is  " + status;
    long when = System.currentTimeMillis();
    Notification notification = new Notification(icon, tickerText, when);
    String expandedText = "Phone Tracker is  " + status;
    String expandedTitle = "Phone Tracker is " + status;
    // launchIntent = PendingIntent.getActivity(context, notificationRef,
    // intentSetRamark, 0);
    notification.setLatestEventInfo(getApplicationContext(), expandedTitle,
        expandedText, null);
    notificationManager.notify(1, notification);
    notificationManager.cancel(1);
}

强制关闭日志

11-08 09:06:29.769: E/AndroidRuntime(359): FATAL EXCEPTION: main
11-08 09:06:29.769: E/AndroidRuntime(359): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mlost2000free/com.mlost2000free.LostPhoneSettingsActivity}: java.lang.IllegalArgumentException: contentIntent required: pkg=com.mlost2000freeid=1 notification=Notification(vibrate=null,sound=null,defaults=0x0)
11-08 09:06:29.769: E/AndroidRuntime(359):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
11-08 09:06:29.769: E/AndroidRuntime(359):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-08 09:06:29.769: E/AndroidRuntime(359):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-08 09:06:29.769: E/AndroidRuntime(359):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-08 09:06:29.769: E/AndroidRuntime(359):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-08 09:06:29.769: E/AndroidRuntime(359):  at android.os.Looper.loop(Looper.java:123)
11-08 09:06:29.769: E/AndroidRuntime(359):  at android.app.ActivityThread.main(ActivityThread.java:4627)
11-08 09:06:29.769: E/AndroidRuntime(359):  at java.lang.reflect.Method.invokeNative(Native Method)
11-08 09:06:29.769: E/AndroidRuntime(359):  at java.lang.reflect.Method.invoke(Method.java:521)
11-08 09:06:29.769: E/AndroidRuntime(359):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-08 09:06:29.769: E/AndroidRuntime(359):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-08 09:06:29.769: E/AndroidRuntime(359):  at dalvik.system.NativeStart.main(Native Method)
11-08 09:06:29.769: E/AndroidRuntime(359): Caused by: java.lang.IllegalArgumentException: contentIntent required: pkg=com.mlost2000free id=1 notification=Notification(vibrate=null,sound=null,defaults=0x0)
11-08 09:06:29.769: E/AndroidRuntime(359):  at android.os.Parcel.readException(Parcel.java:1251)
11-08 09:06:29.769: E/AndroidRuntime(359):  at android.os.Parcel.readException(Parcel.java:1235)
11-08 09:06:29.769: E/AndroidRuntime(359):  at android.app.INotificationManager$Stub$Proxy.enqueueNotificationWithTag(INotificationManager.java:274)
11-08 09:06:29.769: E/AndroidRuntime(359):  at android.app.NotificationManager.notify(NotificationManager.java:110)
11-08 09:06:29.769: E/AndroidRuntime(359):  at android.app.NotificationManager.notify(NotificationManager.java:90)
11-08 09:06:29.769: E/AndroidRuntime(359):  at com.mlost2000free.LostPhoneSettingsActivity.showNotification(LostPhoneSettingsActivity.java:429)
11-08 09:06:29.769: E/AndroidRuntime(359):  at com.mlost2000free.LostPhoneSettingsActivity.setStatus(LostPhoneSettingsActivity.java:400)
11-08 09:06:29.769: E/AndroidRuntime(359):  at com.mlost2000free.LostPhoneSettingsActivity.onCreate(LostPhoneSettingsActivity.java:76)
11-08 09:06:29.769: E/AndroidRuntime(359):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-08 09:06:29.769: E/AndroidRuntime(359):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)

问题是什么。

1 个答案:

答案 0 :(得分:3)

您应该将意图传递给通知。另外值得注意的是,您正在使用一些弃用的方法,即setLatestEventInfo,它可能值得转换为http://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html,因为这可能会减少您向前发展的问题。