2
最近我開始玩Kotlin語言,我嘗試運行一個非常簡單的應用程序。我有兩個文件:MainActivity.kt和幾個kotlin方法的VersionSupport.kt。Android 4.x由Kotlin方法引起的VerifyError
我對使用Android 5.x和6.0的Nexus 5(真實設備和模擬器)上的應用程序沒有任何問題。我嘗試在4.x版本的操作系統上運行應用程序時出現問題。
在我的MainActivity:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
textView.apply { text = "Hello kotlin!" }
supportsKitKat { longToast("kitKat") }
}
這裏是支持的方法實現
public fun supportsKitKat(code:() -> Unit) {
supportsVersion(code, Build.VERSION_CODES.KITKAT)
}
private fun supportsVersion(code:() -> Unit, sdk: Int) {
if(Build.VERSION.SDK_INT >= sdk) {
code.invoke();
}
我使用Android 2.0的工作室預覽3b和科特林1.0.0-β-4583
FATAL EXCEPTION: main java.lang.VerifyError: pl/kpob/bootstrap/utils/VersionSupportKt
at pl.kpob.bootstrap.MainActivity.onCreate(MainActivity.kt:21)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)