2016-03-30 14 views
-2

PendingIntentを取得しようとしていますが、通知ドロワーのnotificationをクリックするたびにアプリケーションがクラッシュします。通知バー(PendingIntent)で通知がクリックされた後のNullPointerException

だから、基本的に、私はCustomerCurrentlyServing.class、それがロードされたとき、それは前のアクティビティから渡されたすべての文字列を取得すると呼ばれるこのJavaクラスを以下に示しているよう:

queueNo = String.valueOf(getIntent().getExtras().getInt(Constants.EX_MSG_QUEUE_NO)); 
    queueKey = getIntent().getExtras().getString(Constants.EX_MSG_QUEUE_KEY); 
    shopName = getIntent().getExtras().getString(Constants.EX_MSG_SHOP_NAME); 
    shopKey = getIntent().getExtras().getString(Constants.EX_MSG_SHOP_KEY); 
    customerid = getIntent().getExtras().getString(Constants.EX_MSG_CUSTOMER_ID); 

は私がする方法を実装しました

public void showNotification() { 
    Intent intent = new Intent(getApplicationContext(), CustomerCurrentServing.class); 
    PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent(this, CustomerCurrentServing.class), 0);intent.putExtra(Constants.EX_MSG_QUEUE_NO,queueNo); 
     intent.putExtra(Constants.EX_MSG_QUEUE_NO,queueNo); 
    intent.putExtra(Constants.EX_MSG_QUEUE_KEY,queueKey); 
    intent.putExtra(Constants.EX_MSG_SHOP_NAME,shopName); 
    intent.putExtra(Constants.EX_MSG_SHOP_KEY,shopKey); 
    Resources r = getResources(); 
    Notification notification = new NotificationCompat.Builder(this) 
      .setTicker(r.getString(R.string.notification_title)) 
      .setSmallIcon(android.R.drawable.ic_menu_report_image) 
      .setContentTitle(r.getString(R.string.notification_title)) 
      .setContentText(r.getString(R.string.notification_text)) 
      .setContentIntent(pi) 
      .setAutoCancel(true) 
      .build(); 

    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
    notificationManager.notify(0, notification); 

ユーザが通知ドロワーの通知をクリックすると、基本的に同じクラスが開きます。このエラーを解決する方法を

 queueNo = String.valueOf(getIntent().getExtras().getInt(Constants.EX_MSG_QUEUE_NO)); 

任意のアイデア:しかし、それは私が通知をクリックすると、エラーがライン上NullPointerExceptionがある時はいつでもクラッシュしますか?私はこのエラーを1週間解決しようとしていて、あなたの助けに感謝しています。

+0

'PendingIntent.getActivity()'に渡す 'Intent'に追加項目を追加する必要があります。そして**あなたが 'PendingIntent.getActivity()'を呼び出す前に**それらを追加する必要があります。小さな太陽からの答えを見てください。 –

答えて

0
PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent(this, CustomerCurrentServing.class), 0); 

ここにあなたがIntent

の新しいインスタンスを渡しているあなたはすでに合格しなければならないが、すなわち

Intent intent = new Intent(getApplicationContext(), CustomerCurrentServing.class); 
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0); 

インスタンスを作成し、PendingIntentオブジェクトを作成する前にすべてのそれらのputExtrasを書きます。

+0

こんにちは、あなたのアドバイスに従ってコードを修正しましたが、同じNullPointerExceptionエラーが表示されています:( – purplewind

+0

あなたが渡しているかどうかを確認する必要がありますか、getIntent()を試してみてください。 hasExtra(Constants.EX_MSG_QUEUE_KEY) 'と他のすべての値 – Rocky

1

あなたがputExtraはテントの上に、あなたは間違った意図にエキストラを追加するコードのこの部分ではPendingIntent

Intent intent = new Intent(getApplicationContext(), CustomerCurrentServing.class); 
    intent.putExtra(Constants.EX_MSG_QUEUE_NO,queueNo); 
    intent.putExtra(Constants.EX_MSG_QUEUE_NO,queueNo); 
    intent.putExtra(Constants.EX_MSG_QUEUE_KEY,queueKey); 
    intent.putExtra(Constants.EX_MSG_SHOP_NAME,shopName); 
    intent.putExtra(Constants.EX_MSG_SHOP_KEY,shopKey); 
    PendingIntent pi = PendingIntent.getActivity(this, 0, intent , 0); 
0

に渡す必要があり、あなたは、あなたが渡す新しいものを作成する意図を作成しています保留中の意図に!あなたがこれを行う場合は、エキストラを入れている PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent(this, CustomerCurrentServing.class), 0); intent.putExtra(Constants.EX_MSG_QUEUE_NO,queueNo);

Intent intent = new Intent(getApplicationContext(), CustomerCurrentServing.class); PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0); intent.putExtra(Constants.EX_MSG_QUEUE_NO,queueNo); intent.putExtra(Constants.EX_MSG_QUEUE_KEY,queueKey); intent.putExtra(Constants.EX_MSG_SHOP_NAME,shopName); intent.putExtra(Constants.EX_MSG_SHOP_KEY,shopKey);

そしてないあなたの保留中の意思で行ったように、新たな意図を作成する:あなたがやるべきことは何

はこれです最初のインテントは、保留中のインテントに無制限に新しいものを渡します。したがって、ユーザーが通知をクリックしてアクティビティを実行すると、アクティビティは試してエクストラを取得し、nullポインタを与えます。

関連する問題