2016-08-17 12 views
0

AndroidのNotificationManager APIを使用して通知を送信しようとしています。以下は、私のコードは次のとおりです。通知マネージャが正しく初期化されていても通知を作成できません

のAndroidManifest.xml:

<uses-sdk 
    android:minSdkVersion="23" 
    android:targetSdkVersion="23" /> 
<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
... 
    <activity 
     android:name="com.tabs.activity.Comments" 
     android:configChanges="orientation|keyboardHidden" 
     android:label="View Post" 
     android:theme="@style/AppTheme.NoActionBar" 
     android:windowSoftInputMode="stateAlwaysHidden|adjustResize" > 
    </activity> 
    <service 
     android:name="com.tabs.activity.NotificationService"> 
     <intent-filter> 
      <action android:name="com.google.firebase.MESSAGING_EVENT"/> 
     </intent-filter> 
    </service> 
... 

Comments.java:

public void createComment(){ 
    ... 
    NotificationService notificationService = new NotificationService(); 
    notificationService.showNotification(getApplicationContext(), R.id.commenter_profile_photo, "User commented: Hi!", "User"); 
    ... 
} 

NotificationService.java:(スタックトレース)に示されている

public void showNotification(Context context, int commenterId, String messageBody, String commenter){ 
    NotificationCompat.Builder notifiationBuilder = new NotificationCompat.Builder(this) 
      .setContentTitle("Title") 
      .setContentText(messageBody) 
      .setTicker("New comment from " + commenter) 
      .setSmallIcon(commenterId); 

    Intent moreInfoIntent = new Intent(context, news_feed.class); 

    //When the user clicks back, it doesn't look sloppy! 
    TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(context); 
    taskStackBuilder.addNextIntent(moreInfoIntent); 

    //If the intent already exists, just update it and not create a new one 
    PendingIntent pendingIntent = taskStackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); 

    //When the notification is actually clicked on 
    notifiationBuilder.setContentIntent(pendingIntent); 

    //Notification manager to notify of background event 
    notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE); 

    notificationManager.notify(1, notifiationBuilder.build()); 
} 

エラー:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference 
                  at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:160) 
                  at android.app.Notification$Builder.<init>(Notification.java:2287) 
                  at android.support.v4.app.NotificationCompatApi21$Builder.<init>(NotificationCompatApi21.java:68) 
                  at android.support.v4.app.NotificationCompat$NotificationCompatImplApi21.build(NotificationCompat.java:759) 
                  at android.support.v4.app.NotificationCompat$Builder.build(NotificationCompat.java:1559) 
                  at com.tabs.activity.NotificationService.showNotification(NotificationService.java:113) 
                  at com.tabs.activity.Comments.createComment(Comments.java:146) 

このコードの問題は、何らかの理由でnotificationManager.notify(1, notifiationBuilder.build());行が常にNullPointerExceptionを生成することです。私は彼らが何であるか知っていますが、なぜ私の文脈にはgetApplicationInfo()が必要なのか分からないので、私はこの問題を解決できないようです。私の文脈は、私を混乱させるComments.javaからそれを渡すときに既にそれを持っているはずです。私は、通知マネージャをどうやって間違って構築できたか分かりません。以下は、与えられたスタックトレースです。どんな助けもありがとう。ありがとう!

+0

さて、この質問に見えるものすごく身近な...それは重複としてマークされていたので、私はそれがされているはずはないと思うとき – shmosel

+0

@shmoselそれはでした。私はタイトルが私のせいだと思う少し誤解を招くことが原因だと思う。 – user1871869

+0

コンテキストが正しく初期化されていないようですか?一度デバッグしてください – 7geeky

答えて

0

変更

NotificationCompat.Builder notifiationBuilder = new NotificationCompat.Builder(this) 

NotificationCompat.Builder notifiationBuilder = new NotificationCompat.Builder(context) 
+0

wokinr fin私の場合。私は、通知を使用しています通知=新しいNotificationCompat.Builder(getBaseContext()) .setContentIntent(申請中) .setStyle(新NotificationCompat.BigTextStyle()。bigText(ボディ)) .setTicker(タイトル) .setContentTitle(サブジェクト) .setContentText(本体) )((真) .setSmallIcon(R.drawable.d2h_icon) .setAutoCancel(真) .build .setOnlyAlertOnce。 – hitesh141

関連する問題