【问题标题】:Android App crashes when push is received and app is closed收到推送并关闭应用程序时,Android 应用程序崩溃
【发布时间】:2013-09-28 12:28:32
【问题描述】:

我正在使用 cordova 开发 Android 和 iOS 应用程序。当前版本有 2.2.0。我有以下 Java 代码来显示推送通知:

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

当应用程序在后台运行时,代码运行良好。但如果应用程序完全关闭,我会收到以下 LogCat 错误,并且不会显示推送:

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

我真的不能说空指针异常来自哪里,所以任何提示都将受到高度赞赏。

【问题讨论】:

  • contextnull 因为MainActivity.getAppContext(); 返回null ...您必须了解活动生命周期... 编辑:putNotification(String title, String message) 更改为putNotification(Context context, String title, String message) 并使用方法参数中的 context ... 作为上下文,您也可以传递 Service
  • 提示:context 为空。
  • @Selvin:哇,就这么简单。非常感谢!如果您将此作为答案,您也会因此获得声誉;-)

标签: android cordova nullpointerexception push google-cloud-messaging


【解决方案1】:

在您的代码中 java.lang.NullPointerException 由于空上下文而发生异常..

所以,使用 onMessage 方法中传递的上下文

 protected void onMessage(final Context context, Intent intent) 

在您的 GCMIntentService 类中使用此上下文

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-06
    • 1970-01-01
    • 2012-11-06
    • 1970-01-01
    相关资源
    最近更新 更多