java - 需要帮助处理由 java.lang.IncompatibleClassChangeError 引起的致命异常

标签 java android android-studio google-cloud-messaging

所以,就在昨天,我在 Android Studio 中遇到了这个我以前从未见过的错误,这是由过去 3 个月未触及的一段代码引起的。这来自用于注册推送通知的代码,本周早些时候运行良好,但现在导致此错误:

05-20 10:37:02.064 22471-22681/com.appname E/AndroidRuntime: FATAL EXCEPTION: IntentService[RegIntentService]
                                                               Process: com.appname, PID: 22471
                                                               java.lang.IncompatibleClassChangeError: The method 'java.io.File android.support.v4.content.ContextCompat.getNoBackupFilesDir(android.content.Context)' was expected to be of type virtual but instead was found to be of type direct (declaration of 'java.lang.reflect.ArtMethod' appears in /system/framework/core-libart.jar)
                                                                   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.appname.services.RegistrationIntentService.onHandleIntent(RegistrationIntentService.java:32)
                                                                   at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
                                                                   at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                   at android.os.Looper.loop(Looper.java:135)
                                                                   at android.os.HandlerThread.run(HandlerThread.java:61)

这是我的 RegistrationIntentService 类:

package com.appname.services;

import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import com.google.android.gms.gcm.GoogleCloudMessaging;
import com.google.android.gms.iid.InstanceID;
import com.appname.MainSharedPreferences;
import com.appname.R;
import java.io.IOException;

public class RegistrationIntentService extends IntentService {

    private static final String TAG = "RegIntentService";

    public RegistrationIntentService() {
        super(TAG);
    }
    public static void startService(final Context context) {
        final Intent intent = new Intent(context, RegistrationIntentService.class);
        context.startService(intent); //HERE is the line that is causing the crash
    }

    @Override
    public void onHandleIntent(Intent intent) {
        InstanceID instanceID = InstanceID.getInstance(this);
        try {
            String token = instanceID.getToken(getString(R.string.gcm_defaultSenderId) , GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
            MainSharedPreferences.saveGCMInstanceToken(token, getApplicationContext());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

这是从我的 MainActivity 类调用上述类的代码行:

RegistrationIntentService.startService(getApplicationContext());

关于我的 MainActivity 的一些信息,如果有帮助...

public class MainActivity extends SocialActivity implements SeekBar.OnSeekBarChangeListener

public abstract class SocialActivity extends AppCompatActivity implements GraphRequest.GraphJSONObjectCallback

我试过跳回到几个较早的提交,但得到了同样的错误。这让我相信我的 Android Studio 或 Java 版本可能有问题。我更新了我的java版本并重新安装了Android Studio,仍然是同样的错误。也用旧版本在单独的计算机上尝试过,仍然是同样的错误。

我什至尝试根据 this guide 从 GCM 迁移到 Firebase并最终再次遇到同样的错误。

我可以注释掉导致应用程序崩溃的行,它会正常运行,但之后我将不再收到通知。

如有任何帮助或建议,我们将不胜感激!

编辑:这也是我的相关编译/类路径语句...

在 appname build.gradle 中:

buildscript {
    repositories {
        jcenter()
    }
    repositories { mavenCentral() }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.0'
        classpath 'com.google.gms:google-services:3.0.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

在应用程序 build.gradle 中:

apply plugin: 'com.android.application'
apply plugin: 'io.fabric'

repositories {
    //...
}

allprojects {
    //...
}

android {
   //...
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com.google.code.gson:gson:2.5'
    compile 'com.android.support:design:23.4.0'
    compile 'com.android.support:recyclerview-v7:+'
    compile "com.google.firebase:firebase-messaging:9.0.0"
    //...
}

apply plugin: 'com.google.gms.google-services'

最佳答案

5 月 27 日更新:

我们刚刚发布了一个更新(版本 9.0.1)来修复我在第一次编辑中提到的不兼容问题。
请更新您的依赖项,如果这仍然是一个问题,请告诉我们。

谢谢!


5 月 20 日原始答案:

您遇到的问题是由于
play-services/firebase sdk v9.0.0com.android.support:appcompat-v7 >= 24
(随android-N sdk发布的版本)

您应该能够通过针对早期版本的支持库来修复它。喜欢:

compile 'com.android.support:appcompat-v7:23.4.0'

关于java - 需要帮助处理由 java.lang.IncompatibleClassChangeError 引起的致命异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37352124/

相关文章:

java - 尝试在 Android Studio 中打开 Unity Activity 会产生 android.content.res.Resources$NotFoundException : String resource ID #0x0

安卓 : cannot build project after upgrading Android Studio to 2. 3

Java 和文件列表

android - FirebaseUI 安装错误

java - 如何格式化 AJAX POST 请求与 cURL?

java.lang.NoClassDefFoundError : Failed resolution of: Landroid/support/v4/graphics/drawable/DrawableWrapper; 错误

Android Camera2 API 预览有时会失真

android - E/LoadedApk : Unable to instantiate appComponentFactory only on Android Q (API 29)

java - 更改 ListView

java - 网络编程中有哪些类型的套接字或方法来创建 VoIP 或在线游戏