1

我一直在寻找我面临的问题,但还没有找到任何与我尝试从 GoogleCloudMessaging 获取 InstanceID 相关的信息。我尝试在扩展 IntentService 的类中设置调用,并尝试使用 AsyncTask 在后台调用它,但无济于事。

我一直在关注如何设置清单和应用程序以获得正确信息的教程。有人知道可能出了什么问题吗?我的日志说 InstanceID.getInstance(getApplicationContext()); 处有一个 nullPointerException

我的电话:

private class RegisterGCM extends IntentService{
    public RegisterGCM(){
        super("GcmIntentService");
        registerDevice();
    }
    private void registerDevice(){
        InstanceID instanceId = InstanceID.getInstance(getApplicationContext());

        //Constants contains GCM_SENDER_ID which is the project number from Google Developer Console
        String token = instanceId.getToken(Constants.GCM_SENDER_ID, GoogleCloudMessaging.INSTANCE_ID_SCOPE);
    }
}

登录后,我将这个类放在我的 LandingPage 类中,并通过以下方式调用它:

new RegisterGCM();

在检查播放服务是否是最新的并更新后立即调用。

有任何想法吗?如果我需要提供更多信息,我可以,但这只是我可以发布的一小部分。

编辑:尝试将意图过滤器添加到清单中的服务并检查 getApplicationContext() 是否为空,这就是返回的异常告诉我的内容:

06-15 16:42:38.614: W/System.err(17295): java.lang.NullPointerException:   Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
06-15 16:42:38.624: W/System.err(17295):    at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:110)
06-15 16:42:38.624: W/System.err(17295):    at com.google.android.gms.iid.InstanceID.zza(Unknown Source)
06-15 16:42:38.624: W/System.err(17295):    at com.google.android.gms.iid.InstanceID.getInstance(Unknown Source)
06-15 16:42:38.624: W/System.err(17295):    at .gcmnotification.GcmIntentService.registerDevice(GcmIntentService.java:33)
06-15 16:42:38.624: W/System.err(17295):    at .gcmnotification.GcmIntentService.<init>(GcmIntentService.java:29)
06-15 16:42:38.624: W/System.err(17295):    at .LandingPageActivity.onCreate(LandingPageActivity.java:115)
06-15 16:42:38.624: W/System.err(17295):    at android.app.Activity.performCreate(Activity.java:6289)
06-15 16:42:38.624: W/System.err(17295):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
06-15 16:42:38.624: W/System.err(17295):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
06-15 16:42:38.624: W/System.err(17295):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2777)
06-15 16:42:38.624: W/System.err(17295):    at android.app.ActivityThread.access$900(ActivityThread.java:179)
06-15 16:42:38.624: W/System.err(17295):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1462)
06-15 16:42:38.624: W/System.err(17295):    at android.os.Handler.dispatchMessage(Handler.java:102)
06-15 16:42:38.624: W/System.err(17295):    at android.os.Looper.loop(Looper.java:145)
06-15 16:42:38.624: W/System.err(17295):    at android.app.ActivityThread.main(ActivityThread.java:5972)
06-15 16:42:38.624: W/System.err(17295):    at java.lang.reflect.Method.invoke(Native Method)
06-15 16:42:38.624: W/System.err(17295):    at java.lang.reflect.Method.invoke(Method.java:372)
06-15 16:42:38.624: W/System.err(17295):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
06-15 16:42:38.624: W/System.err(17295):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)

这是从我的 catch 块返回的错误消息:尝试在空对象引用上调用虚拟方法 'android.content.Context android.content.Context.getApplicationContext()'

所以我猜 getApplicationContext 返回null?

4

2 回答 2

1

根据指南,确保您的 IntentService 具有

<intent-filter>
             <action android:name="com.google.android.gms.iid.InstanceID"/>
</intent-filter>

允许。另外,你什么时候打电话给服务?如果您没有在清单中明确将其作为exported=false 启动, getApplicationContext()则可能返回null。单步执行它会被混淆的代码,但看起来你最好使用对服务上下文的引用,因为它getApplicationContext()从你传入的上下文中调用它的几个步骤,这很可能是你的空对象所在的位置来自(哪里。

我认为这个调用为Application该类返回 null 。

看看这里的构造函数: Application 类,然后它是超级的:ContextWrapper,然后调用getApplicationContext()调用mBase对象,看起来它是空的,因此是 NPE。

于 2015-06-15T19:47:51.943 回答
1

您不应该创建 RegisterGCM() 的实例。它应该从您的“登陆页面”调用 startService() 开始:

if (checkPlayServices()) {
    // Start IntentService to register this application with GCM.
    Intent intent = new Intent(this, RegisterGCM.class);
    startService(intent);
}

在此处查看完整示例

于 2015-06-18T01:14:08.703 回答