18

変更する必要があるというメッセージを受け取ったときに、通知バーの通知を更新するサービスを実行しています。

ただし、通知を更新するときに次のエラーが発生することがあります

java.lang.IllegalArgumentException: contentIntent required

これが私のコードです:

可変設定


int icon = R.drawable.notification;
CharSequence tickerText = "Test";
long when = System.currentTimeMillis();
PendingIntent contentIntent;

Notification notification = new Notification(icon, tickerText, when);

NotificationManager mNotificationManager;

NotificationManager の作成


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

通知の作成


    Intent notificationIntent = new Intent(this, TestsApp.class);
    contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
    notification.flags |= Notification.FLAG_NO_CLEAR;
    notification.icon = R.drawable.notification3;
    notification.setLatestEventInfo(this, "Registering", "Test", contentIntent);
    mNotificationManager.notify(1, notification);

お知らせの更新


    notification.icon = R.drawable.notification2;
    notification.setLatestEventInfo(getApplicationContext(), "Registered", "Test", contentIntent);
    mNotificationManager.notify(1, notification);   

contentIntent のどこかで何かが起こっていますが、それは正しいでしょうか?

Service クラスの先頭でメンバー変数として宣言されており、上記以外のコードでは使用されていないため、どこで null にリセットされるのでしょうか?

4

4 に答える 4

15

通知のcontentIntentを設定する必要があります。

あなたの場合:

notification.contentIntent = notificationIntent;

そうしないと、通知のcontentIntentが設定されていないため、nullであるというメッセージが表示されます。

ドキュメントはここにあります:http://developer.android.com/reference/android/app/Notification.html#contentIntent

ここに小さな例があります:http://united-coders.com/nico-heid/show-progressbar-in-notification-area-like-google-does-when-downloading-from-android

于 2010-09-13T15:39:10.673 に答える
3

これはAndroid OSのバージョンが原因だと思います

バージョン2.3 以下では、 contentIntentを設定する必要があります。そうでない場合、この例外が発生します。

私のプロジェクトでは、次のように書きます。

 if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB) { 
     Intent intent = new Intent();
     PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, 0); 
     mNotification.contentIntent = contentIntent; 
 }

おそらくこれはあなたを助けることができます!

于 2015-05-15T09:20:50.583 に答える
2

あなたの場合

contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,0);

同じアクションで異なるエクストラでインテントを使用したい場合:

  1. requestCodeデフォルトから"0"の変更

    getActivity(Context context, int requestCode, Intent intent, int flags)
    

のようなユニークなものに

(int) System.currentTimeMillis();
notification.contentIntent = notificationIntent;

次の理由により、両方の手順が必須です。

  • オプション 2 は、オプション 1 なしでは機能しません。
  • オプション 1 は、2 なしで IllegalArgumentException をスローします。
于 2011-11-29T22:19:50.503 に答える
0

私の場合、作成する単一の通知を使用して実行するサンプルコードがあり、「contentIntentが必要です」というエラーも発生しました-Googleは私をこのスレッドに連れて行きました:D

この問題の原因は、サンプルコードからコピーしてEclipseプロジェクトに貼り付けた引用でした。「 」を削除して再度入力すると、問題は解決しました。多分これは誰かを助けるでしょう。

これらはエラーの引用元でした: nb.setContentTitle("My first notifications!"); nb.setContentText("こんにちは");

于 2013-06-15T21:26:10.710 に答える