Kotlin: Convert Java to Kotlin issue
這也讓我知道我們工程師非常不受尊重,
當我們選擇新創公司工作就是這樣,工作很容易不保,有時不是自己的問題,
遇到不好的主管或同事都是家常便飯.
-----
----
很方便快速,但當Java轉換Kotlin 後,
我就發生執行app發生error:
Caused by: java.lang.ClassNotFoundException: Didn't find class
但這個file是之前從java 轉換到kotlin.
很神奇的是這只有在執行時才會出錯, compile 時卻可以通過android studio 編譯.
-----
solution:
從原本的java 專案在經歷轉換到Kotlin後,需要新增一些build.gradle code,不然會出現此類錯誤:
buildscript {
ext.kotlin_version = '1.7.10'
repositories {
mavenCentral()
google()
jcenter()
maven {
url 'https://maven.google.com/'
name 'Google'
}
maven { url "https://jitpack.io" }
mavenLocal()
}
dependencies {
classpath 'com.android.tools.build:gradle:7.2.1'
classpath 'com.google.gms:google-services:4.+'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
另一個build.gradle要加:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'com.google.gms.google-services'
android {
....
}
這樣重新build後再執行就可以了.
----
android studio在轉換java到Kotlin, 另一個問題:
2022-07-20 11:51:59.515 6652-6652/com.mark.electricitybillcalculator E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkNotNullParameter, parameter arg1
at com.mark.electricitybillcalculator.fragment.Fragment_elec_fee_home_degree$onCreateView$2.onItemSelected
還是會當機!!!
solution:
看了之後原來是: spinner_months1!! -->要換成spinner_months1?
// 設定adapter
spinner_months1!!.adapter = adapter_months1
spinner_months1!!.onItemSelectedListener =
object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(
arg0: AdapterView<*>?,
arg1: View,
position: Int,
arg3: Long
) {
b_month1_is_summer = func_elec_price.check_is_summary(position + 1)
if (ck_fee_follow_tw_power_corp!!.isChecked) {
fee_desctiption_was_change(rootView, true)
}
func_elec_price.hideSoftKeyboard(activity)
calcute_fee_result()
}
override fun onNothingSelected(arg0: AdapterView<*>?) {
// TODO Auto-generated method stub
}
}
改了之後就不會當了.
// 設定adapter
spinner_months1?.adapter = adapter_months1
spinner_months1?.onItemSelectedListener =
object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(
arg0: AdapterView<*>?,
arg1: View,
position: Int,
arg3: Long
) {
b_month1_is_summer = func_elec_price.check_is_summary(position + 1)
if (ck_fee_follow_tw_power_corp!!.isChecked) {
fee_desctiption_was_change(rootView, true)
}
func_elec_price.hideSoftKeyboard(activity)
calcute_fee_result()
}
override fun onNothingSelected(arg0: AdapterView<*>?) {
// TODO Auto-generated method stub
}
}
這樣就不會當了.
因為spinner_months1在剛開始初始化時有可能是null, 而!!運算子會強制執行, 當然遇到null就當掉.所以改用? ,當spinner_months1未完成初始化時若是null 時就不要執行.if (pair != null && pair.first != null) {
Log.d(SystemProperties.TAG, "total elec fee day=" + Math.round(pair.first!!))
}
if (pair?.first != null) {很簡單對吧?!
Log.d(SystemProperties.TAG, "total elec fee day=" + Math.round(pair.first!!))
}
To perform a certain operation only for non-null values, you can use the safe call operator together with let:
public class appdefine {轉換後變成:
public static final int RESULT_CODE_APP_UPDATE = 0x9992;
}
object appdefine {public class變成了 object
const val RESULT_CODE_APP_UPDATE = 0x9992
}
留言
張貼留言