java GCM 取消注册导致应用程序崩溃

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40218025/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 05:00:35  来源:igfitidea点击:

GCM unregister causing the application to crash

javaandroidgoogle-cloud-messaging

提问by Zeeshan Ali

I've implemented GCM notifications on my app. I am now trying to un-register the app when the user logs out. I am using the following code. When this code executes, it causes the application to crash with the following logcat:

我已经在我的应用程序上实现了 GCM 通知。我现在试图在用户注销时取消注册该应用程序。我正在使用以下代码。当此代码执行时,它会导致应用程序崩溃并显示以下 logcat:

java.lang.IllegalAccessError: Method 'void android.support.v4.content.ContextCompat.<init>()' is inaccessible to class 'com.google.android.gms.iid.zzd' (declaration of 'com.google.android.gms.iid.zzd' appears in /data/app/com.example.packagename-1/base.apk)
    at com.google.android.gms.iid.zzd.zzeb(Unknown Source)
    at com.google.android.gms.iid.zzd.<init>(Unknown Source)
    at com.google.android.gms.iid.zzd.<init>(Unknown Source)
    at com.google.android.gms.iid.InstanceID.zza(Unknown Source)
    at com.google.android.gms.iid.InstanceID.getInstance(Unknown Source)
    at com.zaryans.updatedepoultry.WelcomeActivity.onItemClick(WelcomeActivity.java:469)
    at android.widget.AdapterView.performItemClick(AdapterView.java:310)
    at android.widget.AbsListView.performItemClick(AbsListView.java:1145)
    at android.widget.AbsListView$PerformClick.run(AbsListView.java:3066)
    at android.widget.AbsListView.run(AbsListView.java:3903)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5417)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Here is the code:

这是代码:

InstanceID instanceID = InstanceID.getInstance(WelcomeActivity.this);
try {
   instanceID.deleteInstanceID();
   Utility.logCatMsg("Logged Out Success!!!");
} catch (IOException e) {
   Utility.logCatMsg("Exception while logging out: "+e.getMessage());
   e.printStackTrace();
}

回答by luizMello

I was with same problem after update the support library to 25.0.0 . For me after update the below libs,in the app gradle file, the problem gone.

将支持库更新到 25.0.0 后,我遇到了同样的问题。对我来说,更新以下库后,在应用程序 gradle 文件中,问题就消失了。

compile("com.google.android.gms:play-services-location:9.6.1")
compile("com.google.android.gms:play-services-maps:9.6.1")
compile("com.google.android.gms:play-services-gcm:9.6.1")

回答by AndroidGeek

Update the google play services to the latest version (9.8.0) after updating the android support library to 25.0.0 I had the same problem this morning and this worked for me :)

将 android 支持库更新到 25.0.0 后,将 google play 服务更新到最新版本 (9.8.0) 我今天早上遇到了同样的问题,这对我有用:)

回答by Egor

Not sure this will solve your problem, but it's always a good idea to pass the application Contextto third party frameworks, rather than Activityinstances, as the latter can lead to memory leaks. Try this instead:

不确定这会解决您的问题,但将应用程序传递Context给第三方框架而不是Activity实例总是一个好主意,因为后者可能导致内存泄漏。试试这个:

InstanceID instanceID = InstanceID.getInstance(getApplicationContext());

回答by Shubham Goel

use dependencies in the given way

以给定的方式使用依赖项

compile ("com.google.android.gms:play-services-base:10.0.1") {
        force = true;
    }
    compile ("com.google.android.gms:play-services-maps:10.0.1") {
        force = true;
    }
    compile ("com.google.android.gms:play-services-gcm:10.0.1") {
        force = true;
    }
    compile ('com.google.firebase:firebase-core:10.0.1') {
        force = true;
    }
    compile ('com.google.firebase:firebase-messaging:10.0.1') {
        force = true;
    }