Android应用程序崩溃当推被接收和应用程序被关闭

编程入门 行业动态 更新时间:2024-10-27 01:26:49
本文介绍了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应用程序崩溃当推被接收和应用程序被关闭

本文发布于:2023-06-10 22:56:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/620866.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   Android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!