2016-04-14 43 views
0

Bazı bildirimleri görüntülemek için Android GCM üzerinde çalışıyorum. İyi çalışıyor. Ancak, bildirimi tıkladıktan sonra Ev etkinliğini başlatmamı sağlayabilecek bir kod eklediğimde, çöker. PendingIntent'i kaldırırsam, iyi çalışır, ancak hiçbir işlem yapmaz. Aşağıda android GCM Push bildirimi çöküyor

alıyorum hata günlüğü aşağıda

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

}

kullanarak ve am kod olmasıdır.

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)  

cevap

0

En az onCreate() veya onMessageReceived()

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

onCreate denirdi önce thisContext yok çünkü çöküyor bu iki şey hareket etmelidir.

böyle olmalı:

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()); 
    } 
} 
İlgili konular