2016-04-14 19 views
0

私はアンドロイドGCMで通知を表示しています。それはうまく動作します。しかし、通知をクリックしてHomeアクティビティを開始できるようにするコードを追加すると、クラッシュします。 PendingIntentを削除した場合は正常に動作しますが、何もしません。以下はアンドロイドGCMプッシュ通知がクラッシュする

私は

public class GcmMessageHandler extends GcmListenerService { 
public static final int MESSAGE_NOTIFICATION_ID = 435345; 
Intent intent = new Intent(GcmMessageHandler.this, HomeActivity.class); 

int requestID = (int) System.currentTimeMillis(); //unique requestID to differentiate between various notification with same NotifId 
int flags = PendingIntent.FLAG_CANCEL_CURRENT; // cancel old intent and create new one 
PendingIntent pIntent = PendingIntent.getActivity(this, requestID, intent, flags); 

@Override 
public void onMessageReceived(String from, Bundle data) { 
    String message = data.getString("message"); 

    createNotification(from, message); 
} 

// Creates notification based on title and body received 
private void createNotification(String title, String body) { 
    Context context = getBaseContext(); 

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context) 
      .setSmallIcon(R.mipmap.ic_launcher) 
      .setContentTitle(title) 
      .setContentText(body) 
      .setContentIntent(pIntent); 
    mBuilder.setAutoCancel(true); 

    NotificationManager mNotificationManager = (NotificationManager) context 
      .getSystemService(Context.NOTIFICATION_SERVICE); 
    mNotificationManager.notify(MESSAGE_NOTIFICATION_ID, mBuilder.build()); 
} 

}

を使用して、以下の私が取得していますエラーログでいたコードです。

04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime: FATAL EXCEPTION: main 
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime: Process: abenakebe.yournet.com.abenakebe, PID: 14201 
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime: java.lang.RuntimeException: Unable to instantiate service abenakebe.yournet.com.abenakebe.utils.GcmMessageHandler: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.ContentResolver android.content.Context.getContentResolver()' on a null object reference 
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime:  at android.app.ActivityThread.handleCreateService(ActivityThread.java:3623) 
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime:  at android.app.ActivityThread.access$2000(ActivityThread.java:198) 
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime:  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1759) 
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime:  at android.os.Handler.dispatchMessage(Handler.java:102) 
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime:  at android.os.Looper.loop(Looper.java:145) 
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime:  at android.app.ActivityThread.main(ActivityThread.java:6837) 
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime:  at java.lang.reflect.Method.invoke(Native Method) 
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime:  at java.lang.reflect.Method.invoke(Method.java:372) 
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime:  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404) 
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime:  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199) 
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.ContentResolver android.content.Context.getContentResolver()' on a null object reference 
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime:  at android.content.ContextWrapper.getContentResolver(ContextWrapper.java:101) 
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime:  at android.app.PendingIntent.getActivity(PendingIntent.java:286) 
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime:  at android.app.PendingIntent.getActivity(PendingIntent.java:252) 
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime:  at abenakebe.yournet.com.abenakebe.utils.GcmMessageHandler.<init>(GcmMessageHandler.java:25) 
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime:  at java.lang.reflect.Constructor.newInstance(Native Method) 
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime:  at java.lang.Class.newInstance(Class.java:1684) 
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime:  at android.app.ActivityThread.handleCreateService(ActivityThread.java:3620) 
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime:  at android.app.ActivityThread.access$2000(ActivityThread.java:198)  
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime:  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1759)  
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime:  at android.os.Handler.dispatchMessage(Handler.java:102)  
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime:  at android.os.Looper.loop(Looper.java:145)  
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime:  at android.app.ActivityThread.main(ActivityThread.java:6837)  
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime:  at java.lang.reflect.Method.invoke(Native Method)  
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime:  at java.lang.reflect.Method.invoke(Method.java:372)  
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime:  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)  
04-14 23:24:59.165 14201-14201/abenakebe.yournet.com.abenakebe E/AndroidRuntime:  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)  

答えて

0

あなたは、少なくともonCreate()またはonMessageReceived()

Intent intent = new Intent(GcmMessageHandler.this, HomeActivity.class); 
PendingIntent pIntent = PendingIntent.getActivity(this, requestID, intent, flags); 

onCreateが呼び出される前にthisContextを持っていないので、それがクラッシュするには、この二つのことを移動する必要があります。

は次のようにする必要があります。

public class GcmMessageHandler extends GcmListenerService { 
    public static final int MESSAGE_NOTIFICATION_ID = 435345; 
    Intent intent; 

    int requestID = (int) System.currentTimeMillis(); //unique requestID to differentiate between various notification with same NotifId 
    int flags = PendingIntent.FLAG_CANCEL_CURRENT; // cancel old intent and create new one 
    PendingIntent pIntent; 

    @Override 
    public void onMessageReceived(String from, Bundle data) { 
     String message = data.getString("message"); 
     intent = new Intent(GcmMessageHandler.this, HomeActivity.class); 
     pIntent = PendingIntent.getActivity(this, requestID, intent, flags); 
     createNotification(from, message); 
    } 

    // Creates notification based on title and body received 
    private void createNotification(String title, String body) { 
     Context context = getBaseContext(); 

     NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context) 
       .setSmallIcon(R.mipmap.ic_launcher) 
       .setContentTitle(title) 
       .setContentText(body) 
       .setContentIntent(pIntent); 
     mBuilder.setAutoCancel(true); 

     NotificationManager mNotificationManager = (NotificationManager) context 
       .getSystemService(Context.NOTIFICATION_SERVICE); 
     mNotificationManager.notify(MESSAGE_NOTIFICATION_ID, mBuilder.build()); 
    } 
} 
関連する問題