【问题标题】:How could Intent be null in onHandleIntent()?onHandleIntent() 中的 Intent 怎么可能为空?
【发布时间】:2013-04-04 10:36:21
【问题描述】:

我的 android 应用程序崩溃了,这是 logcat:-

java.lang.NullPointerException
    at com.google.android.gcm.GCMBaseIntentService.onHandleIntent(GCMBaseIntentService.java:194)
    at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.os.HandlerThread.run(HandlerThread.java:60)

我查看了 android gcm r3 源代码,发现 onHandleIntent() 中的参数意图为空。

这甚至可能吗?如何解决?

(我知道Service.onStartCopmmand返回START_STICKY可以看到空意图 但IntentService.onStartCommand 不使用START_STICKY。)

【问题讨论】:

  • 你怎么能说intent是空的??关注 onHandleIntent() 中的第 194 行,可能还有其他内容正在变为空...
  • 感谢您的回复。因为我有来自 android sdk 的 jar 和源代码。第 194 行是“String action = intent.getAction();”
  • intent 在某些系统上为 null,如果传递的 msg.obj 为 null,则完全正常。那里没有空检查。
  • 考虑将您的声音添加到此错误报告code.google.com/p/gcm/issues/…

标签: android google-cloud-messaging


【解决方案1】:

我认为应用程序在安装期间仅在某些设备中崩溃。这是因为在安装过程中,GCM 服务还会从其他 Google 来源接收到一些 Intent,而您的广播接收器还没有准备好处理这种类型的 Intent

如果你只是想通过推送通知接收你想从服务器中提取的 GCM Intent,那么只需在你的句柄 Intent 调用中使用它。

protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
        //String msg = intent.getStringExtra("message");
        String from=extras.getString("from");
        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
        String messageType = gcm.getMessageType(intent);

        if (!extras.isEmpty()) {

            if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
                sendErrorNotification("Send error: " + extras.toString());
            } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
                sendErrorNotification("Deleted messages on server: " + extras.toString());
                // If it's a regular GCM message, do some work.
            } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
                // This loop represents the service doing some work.
                for (int i = 0; i < 5; i++) {
                    Log.i(TAG, "Working... " + (i + 1) + "/5 @ " + SystemClock.elapsedRealtime());
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                    }
                }
                Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());
                // Post notification of received message.
                // sendNotification("Received: " + extras.toString());
                /*********ERROR IN SOME DEVICES*****************/

                 if(from.equals("google.com/iid"))
                 {
                     //related to google ... DO NOT PERFORM ANY ACTION
                 }
                 else { 
                   //HANDLE THE RECEIVED NOTIFICATION
                     String msg = intent.getStringExtra("message");
                     sendNotification(msg);
                    Log.i(TAG, "Received: " + extras.toString());
                     }
                 /**************************/
            }
        }
        GcmBroadcastReceiver.completeWakefulIntent(intent);
}

【讨论】:

  • 我同意,我的 GCMBroadcastReceiver 不时收到一些空意图,所以我必须检查传入的意图是否为空。我不知道会发生什么,但我在安装过程中得到了一个空意图。
  • 关于如何解决和检查此问题的任何更新?
【解决方案2】:

曾经发生在我身上,我传递了错误的 URI。参考你想要传递的意图的文档。

Intent Documentation here.

【讨论】:

    猜你喜欢
    • 2014-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-09
    • 1970-01-01
    相关资源
    最近更新 更多