Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android GCMRegistrar.onDestroy(上下文)崩溃,接收器未注册_Android_Google Cloud Messaging - Fatal编程技术网

Android GCMRegistrar.onDestroy(上下文)崩溃,接收器未注册

Android GCMRegistrar.onDestroy(上下文)崩溃,接收器未注册,android,google-cloud-messaging,Android,Google Cloud Messaging,我该怎么称呼GCMRegistar.ondestory?目前我的主要活动包括: protected void onDestroy() { GCMRegistrar.onDestroy(this); super.onDestroy(); } 在进行了注册或注销之后,然后停止了主要活动,我得到了以下信息: 08-13 15:43:56.459: E/AndroidRuntime(2389): FATAL EXCEPTION: main 08-13 15:43:56.459: E/A

我该怎么称呼GCMRegistar.ondestory?目前我的主要活动包括:

protected void onDestroy() {
    GCMRegistrar.onDestroy(this);
    super.onDestroy();
}
在进行了注册或注销之后,然后停止了主要活动,我得到了以下信息:

08-13 15:43:56.459: E/AndroidRuntime(2389): FATAL EXCEPTION: main
08-13 15:43:56.459: E/AndroidRuntime(2389): java.lang.RuntimeException: Unable to destroy activity {com.test.android/com.test.android.activities.MainActivity}: java.lang.IllegalArgumentException: Receiver not registered: com.google.android.gcm.GCMBroadcastReceiver@40673a10
08-13 15:43:56.459: E/AndroidRuntime(2389):     at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3090)
08-13 15:43:56.459: E/AndroidRuntime(2389):     at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3155)
08-13 15:43:56.459: E/AndroidRuntime(2389):     at android.app.ActivityThread.access$2100(ActivityThread.java:132)
08-13 15:43:56.459: E/AndroidRuntime(2389):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1071)
08-13 15:43:56.459: E/AndroidRuntime(2389):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-13 15:43:56.459: E/AndroidRuntime(2389):     at android.os.Looper.loop(Looper.java:150)
08-13 15:43:56.459: E/AndroidRuntime(2389):     at android.app.ActivityThread.main(ActivityThread.java:4277)
08-13 15:43:56.459: E/AndroidRuntime(2389):     at java.lang.reflect.Method.invokeNative(Native Method)
08-13 15:43:56.459: E/AndroidRuntime(2389):     at java.lang.reflect.Method.invoke(Method.java:507)
08-13 15:43:56.459: E/AndroidRuntime(2389):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-13 15:43:56.459: E/AndroidRuntime(2389):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-13 15:43:56.459: E/AndroidRuntime(2389):     at dalvik.system.NativeStart.main(Native Method)
08-13 15:43:56.459: E/AndroidRuntime(2389): Caused by: java.lang.IllegalArgumentException: Receiver not registered: com.google.android.gcm.GCMBroadcastReceiver@40673a10
08-13 15:43:56.459: E/AndroidRuntime(2389):     at android.app.LoadedApk.forgetReceiverDispatcher(LoadedApk.java:634)
08-13 15:43:56.459: E/AndroidRuntime(2389):     at android.app.ContextImpl.unregisterReceiver(ContextImpl.java:880)
08-13 15:43:56.459: E/AndroidRuntime(2389):     at android.content.Contexttester.unregisterReceiver(Contexttester.java:331)
08-13 15:43:56.459: E/AndroidRuntime(2389):     at com.google.android.gcm.GCMRegistrar.onDestroy(GCMRegistrar.java:249)
08-13 15:43:56.459: E/AndroidRuntime(2389):     at com.test.android.activities.MainActivity.onDestroy(MainActivity.java:407)
08-13 15:43:56.459: E/AndroidRuntime(2389):     at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3072)
08-13 15:43:56.459: E/AndroidRuntime(2389):     ... 11 more
从mainfest编辑:

<receiver android:name=".receivers.PushReceiver"
  android:permission="com.google.android.c2dm.permission.SEND" >
  <intent-filter>
    <action android:name="com.google.android.c2dm.intent.RECEIVE" />
    <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

    <category android:name="com.test.android" />
  </intent-filter>
</receiver>

<service android:name=".services.PushService" />

我想我已经解决了。在执行GCMRegistar.register(上下文)时,我使用了两种不同的上下文。切换到使用getApplicationContext()而不是这样做效果很好。

另一种方法

GCMRegistrar.onDestroy(getApplicationContext());

您可以发布您的AndroidManifest.xml文件吗?问题似乎源于GCMBroadcastReceiver。如果你出示清单文件,我会尽力帮助你。你太棒了。使用getApplicationContext()正在工作。我希望谷歌能做到这一点。正如连勇指出的,我必须将上下文更改为getApplicationContext(),这两种语言都是:registerReceiver(apimager.getBroadcastReceiver(),new IntentFilter(ACTION));和gcmregistar.onDestroy(getApplicationContext())@Giorgio回答说,问题在于使用不同的上下文,应用上下文应用于所有GCM操作。投票+1你很好,但你能进一步增强答案吗?如果你需要一个更好的答案,我建议你阅读安卓系统的上下文。如果您只需要一个简短且不干净的修复:在所有GCM交互中只使用getApplicationContext()或context.getApplicationContext()(因此永远不要使用“this”变量)。