Android -java.lang.illegalargumentexception:通知によって引き起こされるコンテンツが必要ですか?

StackOverflow https://stackoverflow.com/questions/3112008

質問

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

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

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;

通知マネージャーの作成


    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);   

それで、何かが起こっています。

私のサービスクラスの最上部でメンバー変数として宣言されており、上記以外のコードの他の場所では使用されていないため、どこでnullにリセットできますか?

役に立ちましたか?

解決

通知のためにContentIntentを設定する必要があります。

あなたの場合:

notification.contentIntent = notificationIntent;

それ以外の場合は、通知のコンテンツが設定されていないため、通知のコンテンツがnullであるというメッセージが表示されます。

Docuはここにあります: 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-down from-android

他のヒント

これは、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; }

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

あなたの場合

contentIntent = pendentintent.getactivity(this、0、notificationIntent、0);

同じアクションでの意図を使用したい場合は、異なるエキストラを使用したい場合:

1)変更

requestCode

デフォルト「0」から

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

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

(int) System.currentTimeMillis();

` 2)

notification.contentIntent = notificationIntent;

どちらの手順も必須です。

  • オプション2はオプション1なしでは機能しません。
  • オプション1は、2なしでIllegalargumentExceptionをスローします。

私の場合、作成するコードの例があり、作成するための単一の通知があり、「ContentIntentが必須」エラーも取得しました - Googleがこのスレッドに私をもたらしました:D

この問題の原因は、例コードからコピーしてEclipseプロジェクトに貼り付けた引用でした。 ""を削除し、それらを戻し、問題が解決しました。多分これは誰かを助けます。

これらはエラーの引用源でした:nb.setContentTitle( "私の最初の通知!"); nb.setContentText( "hello");

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top