Kotlin构造函数崩溃
在关于kotlin继承的每一个单独的教程中,我已经看到了一个这样的例子:
class SubClass(pref : String) { fun theSubClassMethod() : String { return "The subclass method" } }
但是,如果我这样使用它,出于某种原因,我得到这个例外:
java.lang.NoClassDefFoundError: Failed resolution of: Lkotlin/jvm/internal/Intrinsics; at abccom.logic.SubClass.<init>(SubClass.kt) at abccom.kotlinplayground.MainActivity.onCreate(MainActivity.kt:39) at android.app.Activity.performCreate(Activity.java:6374) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2767) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2879) at android.app.ActivityThread.access$900(ActivityThread.java:182) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1475) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:145) at android.app.ActivityThread.main(ActivityThread.java:6141) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194) Caused by: java.lang.ClassNotFoundException: Didn't find class "kotlin.jvm.internal.Intrinsics" on path: DexPathList[[zip file "/data/app/abccom.kotlinplayground-1/base.apk"],nativeLibraryDirectories=[/vendor/lib64, /system/lib64]] at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56) at java.lang.ClassLoader.loadClass(ClassLoader.java:511) at java.lang.ClassLoader.loadClass(ClassLoader.java:469) at abccom.logic.SubClass.<init>(SubClass.kt) at abccom.kotlinplayground.MainActivity.onCreate(MainActivity.kt:39) at android.app.Activity.performCreate(Activity.java:6374) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2767) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2879) at android.app.ActivityThread.access$900(ActivityThread.java:182) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1475) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:145) at android.app.ActivityThread.main(ActivityThread.java:6141) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194) Suppressed: java.lang.ClassNotFoundException: kotlin.jvm.internal.Intrinsics at java.lang.Class.classForName(Native Method) at java.lang.BootClassLoader.findClass(ClassLoader.java:781) at java.lang.BootClassLoader.loadClass(ClassLoader.java:841) at java.lang.ClassLoader.loadClass(ClassLoader.java:504) ... 16 more Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available
我能做到这一点的唯一方法就是如果我这样做:
class SubClass(pref : String?) { fun theSubClassMethod() : String { return "The subclass method" } }
注意“?” 问号在参数中,没有它我不能让代码工作,但问题是为什么? 为什么它没有它的工作,以及每个教程如何显示参数没有问号,他们没有提到任何关于它…我错过了什么?
这是我使用它的方式:
val subClass = SubClass("ZZZ")
注意:如果构造函数中的参数是一个Int / Boolean等,它工作正常似乎只发生在对象。
通过将其定义为pref: String
而不是pref: String?
Kotlin在kotlin标准库的Intrinsics
类中实现的构造函数中生成一个空检查。
这个错误很可能是因为无论构建程序是否包含kotlin-stdlib
库。 这意味着所需的类无法找到。