android 代码转换成Kotlin代码时,变量报Property must be initialized or be abstract错误
一般的activity是这样的,定义个输入框变量
通过 Code→Convert Java File to Kotlin File 或者快捷键 Ctrl+Alt+Shift+K将Java代码转换成Kotlin代码
Java代码成功转换成了Kotlin代码,但定义的输入框变量却报错了。查看错误信息是
Property must be initialized or be abstract。
也就是说Kotlin中定义的变量,要么是定义时就初始化,要么就定义成抽象的。但显然这里不能定义成抽象的,那就试着直接初始化。
初始化后,果然不再报错。那就直接运行,却发现App直接停止运行了。查看错误日志,
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at android.content.ContextWrapper.getResources(ContextWrapper.java:85)
at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:74)
很显然,我在初始化控件时,Context还没初始化,导致了空指针异常。说明定义控件时就初始化不能解决刚才Kotlin报的错误。
几番查阅Kotlin语法、搜索资料后,才发现,Kotlin有一个lateinit修饰符。字面意思就可以知道它是表名变量需要在定义后才赋值的。
使用 lateinit修饰后,就可以愉快地玩耍了。
转载于:https://my.oschina.net/u/1048653/blog/917616
- android 代码转换成Kotlin代码时,变量报Property must be initialized or be abstract错误
- Xcode编译错误:Synthesized property 'xxxXXX' must either be named the same as a compatible ivar or mus
- Xcode编译错误:Synthesized property 'xxxXXX' must either be named the same as a compatible ivar or mus
- android 出错信息为:Class 'Anonymous class derived from Handler' must either be declared abstract or imple
- XCode中互相包含引起的"property with 'retain(or strong)' attribute must be of object type"错误与解决
- 如何忽略Android资源编译错误 Error: this attribute must be localized.
- 编译代码报出Android library projects cannot be launched错误的解决
- Android的Switch语句case expressions must be constant expressions错误的解决方法
- svm 错误 Error using ==> svmtrain at 232 Y must be a vector or a character array.
- jgoodies FormLayout 布局错误The row index 12 must be less than or equal to 10.
- 错误:Property or field 'default' cannot be found on object of type 'com.netflix.appinfo.InstanceInfo'
- java.sql.SQLException: The SQL statement must not be null or empty.这个错误
- Android开发出现Java.lang.IllegalStateException的原因;Target host must be null,or set in parameters
- Android自学之路,DrawerLayout must be measured with MeasureSpec.EXACTLY.错误
- Property with 'retain (or strong)' attribute must be of object type
- Android ImageLoader must be init with configuration before using 错误解决方法
- matlab错误:Subscript indices must either be real positive integers or logicals.
- Android switch 中 case expressions must be constant expressions 错误
- android下编摄像头的hal下代码时的错误error: call to '__property_get_too_small_error' declared with attribute error
- 经典错误重现->Java RunTime Environment (JRE) or Java Development Kit (JDK) must be available in order to