【问题标题】:noclassdeffounderror on device设备上的 noclassdeffounderror
【发布时间】:2017-03-22 07:22:07
【问题描述】:

我有相同的代码:

public static void clearCacheWithoutMainActivity()
{
    try
    {
        viewModelForClass.entrySet().removeIf(e -> !e.getKey().equals(className));
    }
    catch(NoClassDefFoundError e)
    {
        int k = 0;
    }
}

private static Map<String, Pair<BaseObservable, Date>> viewModelForClass = new LinkedHashMap<>();

private static final String className = MainActivity.class.getName();

在模拟器 android 中我没有捕捉到错误 NoClassDefFoundError。但是在我的设备中,这个 android 5.1 我发现了这个错误。

    compileSdkVersion 25
    buildToolsVersion "25.0.2"

  defaultConfig {
        applicationId "ca.amikash.cashback"
        minSdkVersion 16
        targetSdkVersion 22
        versionCode 1
        versionName "1.0.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        multiDexEnabled true
    }

    dexOptions {
        preDexLibraries = false
        jumboMode = false
        maxProcessCount 4
        javaMaxHeapSize "6g"
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

我的 build.gradle 文件。这可能是错的?

【问题讨论】:

    标签: java android devise try-catch


    【解决方案1】:

    您也可以分享错误日志会更有帮助。

    您是否编辑了清单文件以在标签中设置 android:name 如下:

    <application android:name="android.support.multidex.MultiDexApplication">
    

    或者,如果您覆盖 Application 类,请将其更改为扩展 MultiDexApplication

    查看此链接了解更多信息:developer.android.com/studio/build/multidex.html

    【讨论】:

    • java.lang.NoClassDefFoundError: ca.amikash.cashback.ViewModelProvider$$Lambda$1 就是这个错误。扩展 MultiDexApplication - 没有帮助我
    • 您是否使用了具有 ca.amikash.cashback.ViewModelProvider 类的库项目?
    • 在类 TheApplication extends MultiDexApplication 我使用 ViewModelProvider.clearCacheWithoutMainActivity();
    • 如果你还没有,你可以尝试清理重建你的项目,然后再次运行应用程序吗?
    • 如果您使用的是retrolambda,这个问题可能与它有关。看看这个github.com/orfjackal/retrolambda/issues/37。许多人报告说干净构建项目已经解决了类似的问题。
    【解决方案2】:
    for(Iterator<Map.Entry<String, Pair<BaseObservable, Date>>> it = viewModelForClass.entrySet().iterator(); it.hasNext(); ) {
                Map.Entry<String, Pair<BaseObservable, Date>> entry = it.next();
                if(!entry.getKey().equals(className)) {
                    it.remove();
                }
            }
    

    相反

       viewModelForClass.entrySet().removeIf(e -> !e.getKey().equals(className));
    

    解决了这个问题。但是还是不明白为什么排除会被抛出。而且没有 lambdas 的代码不漂亮,但是怎么办。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-11
      • 1970-01-01
      • 1970-01-01
      • 2014-07-01
      • 2017-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多