Android应用程序崩溃当推被接收和应用程序被关闭 [英] Android App crashes when push is received and app is closed

查看:242
本文介绍了Android应用程序崩溃当推被接收和应用程序被关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个Android和放大器; iOS应用程序与科尔多瓦。当前版本有2.2.0。我有以下Java code,显示推送通知:

I'm developing an Android & iOS App with cordova. Current Version there is 2.2.0. I've got the following Java code to show the Push Notification:

private void putNotification(String title, String message){
        try{
            Log.w("Push", "putNotification");
            MainActivity context = MainActivity.getAppContext();
            Log.w("Push", "context is set");

                NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
                Log.w("Push", "notificationManager is set");
                Notification note = new Notification(R.drawable.icon, title, System.currentTimeMillis());
            Intent notificationIntent = new Intent(context, MainActivity.class);
                notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
                       Intent.FLAG_ACTIVITY_SINGLE_TOP);
                PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
                note.setLatestEventInfo(context, title, message, pendingIntent);
                note.defaults |= Notification.DEFAULT_SOUND;
            note.defaults |= Notification.DEFAULT_VIBRATE;
            note.defaults |= Notification.DEFAULT_LIGHTS;
            note.flags |= Notification.FLAG_AUTO_CANCEL;
                notificationManager.notify(0, note);
        } catch(Exception e){
            Log.w("Push failed", e); 
        }
    }

当应用程序在后台运行code工作完全正常。但是,如果应用程序完全关闭,我得到以下错误LogCat中,并没有推所示:

When the app is running in the background the code works perfectly fine. But If the app is fully closed, I'm getting the following LogCat Errors, and no push is shown:

 09-23 16:30:24.725: W/Push(3276): putNotification
 09-23 16:30:24.725: W/Push(3276): context is set
 09-23 16:30:24.725: W/Push failed(3276): java.lang.NullPointerException
 09-23 16:30:24.725: W/Push failed(3276):   at ch.seika.lakers.GCMIntentService.putNotification(GCMIntentService.java:105)
 09-23 16:30:24.725: W/Push failed(3276):   at ch.seika.lakers.GCMIntentService.onMessage(GCMIntentService.java:46)
 09-23 16:30:24.725: W/Push failed(3276):   at com.google.android.gcm.GCMBaseIntentService.onHandleIntent(GCMBaseIntentService.java:223)
 09-23 16:30:24.725: W/Push failed(3276):   at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
 09-23 16:30:24.725: W/Push failed(3276):   at android.os.Handler.dispatchMessage(Handler.java:99)
 09-23 16:30:24.725: W/Push failed(3276):   at android.os.Looper.loop(Looper.java:137)
 09-23 16:30:24.725: W/Push failed(3276):   at android.os.HandlerThread.run(HandlerThread.java:61)

105线如下:
NotificationManager notificationManager =(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);

我真的不能说那里的空指针异常是从哪里来的,因此任何暗示将高度AP preciated。

I really can't say where the Null Pointer Exception is coming from, so any hint will be highly appreciated.

推荐答案

在您的code显示java.lang.NullPointerException异常的发生是由于空的上下文。

in your code java.lang.NullPointerException exception occurs due to null context..

因此​​,这是在onMessage方法通过使用上下文

so,use context which is passed in onMessage method

 protected void onMessage(final Context context, Intent intent) 

在您的GCMIntentService类使用该上下文

use this context in your GCMIntentService class

这篇关于Android应用程序崩溃当推被接收和应用程序被关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆