前言
记录下我遇到kotlin的错误与解决方法,希望对你有所帮助
一
错误信息
Kotlin: Cannot access java.io.Serializable’ which is a supertype of ‘kotlin.Int’. Check your module classpath for missing or conflicting dependencies
解决方法
I got the same error today after Intellij upgraded my bundled kotlin plugin on Mac OS. The problem might not be the same, but the error message was identical.
Turns out before my project was using JDK 11, but after the kotlin plugin upgrade the JDK was upgraded as well to 13. I resolved the issue by dropping the existing JDK 11 with the - button in the Project Structure window and then adding it again using the + button - which selected JDK 13 by default. After that this error went away.
将项目结构中sdk旧版全删除,只保留14
如果提示项目没有sdk,再按照提示下载即可
引用
https://discuss.kotlinlang.org/t/cannot-access-java-io-serializable-which-is-a-supertype-of/16595/3
二
错误信息
Error:(9, 1) Kotlin: Conflicting overloads: public fun largerNumber(num1: Int, num2: Int): Int defined in root package in file three.kt, public fun largerNumber(num1: Int, num2: Int): Int defined in root package in file two.kt
Error:(6, 15) Kotlin: Overload resolution ambiguity:
public fun largerNumber(num1: Int, num2: Int): Int defined in root package in file three.kt
public fun largerNumber(num1: Int, num2: Int): Int defined in root package in file two.kt
Error:(3, 1) Kotlin: Conflicting overloads: public fun largerNumber(num1: Int, num2: Int): Int defined in root package in file three.kt, public fun largerNumber(num1: Int, num2: Int): Int defined in root package in file two.kt
D:\IDEA\20200524-1\src\two.kt
D:\IDEA\20200524-1\src\three.kt
原因
不同文件中不能定义相同名称的函数,否则报错
解决
注释掉相同名称的函数
三
错误信息
Overload resolution ambiguity. All these functions match
原因
变量的引用模糊不清
解决
重新定义变量名
四
错误信息
Unresolved reference: name5
解决
只要设置权限修饰符在要调用的类就能解决
五
错误信息
An operation is not implemented: Not yet implemented
原因
没有重写方法
解决
将重写的方法生成的todo删除即可,如下
TODO("Not yet implemented")
总结
todo标识符如果没解决,会有运行时异常
六
错误信息
Too many arguments for public constructor Cellphone( defined in数据类与单例类. Cellphone
原因
构造器Cellphone名称 太多而引发的冲突
解决
将多个Cellphone改名
七
错误信息
Error running ‘app’: Default Activity not found
原因
找不到主程序来进行启动
解决
注意activity标签的前缀和后缀是否在合适的位置
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".FirstActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
八
错误信息
已经有布局,但是无法识别,提示Unresolved reference: right. fragment
解决
复制布局的代码,删除已有布局,用万能键创建布局,粘贴进去,就能识别
九
错误信息
Unresolved reference: from
解决
注意区别标红语句的那一段每个字母的大小写,如果看不出来可以复制别人的相同代码然后粘贴就能识别
十
错误信息
Failed to install the following Android SDK packages as some licences have no been accepted
解决
在下方build,在最下方提示install xxxxxx ,点击安装即可
十一
错误信息
Caused by: java. lang. ClassCastException: android. widget . FrameLayout cannot be cast to com. example . a20200616study . NewsContentFragment
at com.example . a20200616study . NewsContentActivity . onCreate (NewsContentActivity.kt:28)
解决
有多个相同名称的变量,要注意导包,别导错了
若不会用debug,要善于用ait+变量名与ctrl+f来进行查代码