在关于kotlin继承的每一个教程中,我都看到过这样的例子:
loadCompressedDataFromByteArray()
但是,如果我这样使用它,由于某种原因我会得到这个例外:
class SubClass(pref : String) {
fun theSubClassMethod() : String {
return "The subclass method"
}
}
我能让它发挥作用的唯一方法就是我这样做:
java.lang.NoClassDefFoundError: Failed resolution of: Lkotlin/jvm/internal/Intrinsics;
at a.b.c.com.logic.SubClass.<init>(SubClass.kt)
at a.b.c.com.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/a.b.c.com.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 a.b.c.com.logic.SubClass.<init>(SubClass.kt)
at a.b.c.com.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"
}
}
注意:如果构造函数中的参数是Int / Boolean等,它的工作正常似乎只发生在对象上。
答案 0 :(得分:6)
通过将其定义为pref: String
而不是pref: String?
,Kotlin会在构造函数中生成一个null检查,该构造函数在kotlin标准库的Intrinsics
类中实现。
错误很可能是因为构建程序的任何内容都无法包含kotlin-stdlib
库。这意味着找不到所需的类。