facebook twitter hatena line email

「Android/kotlin/javaからkotlinへの変換」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
("Platform declaration clash: The following declarations have the same JVM signature"エラー)
 
行58: 行58:
 
</pre>
 
</pre>
 
プロパティにprivateをつければ良い。
 
プロパティにprivateをつければ良い。
 +
 +
=="type mismatch prequired string found string?"エラーの場合==
 +
 +
Log.e(TAG, ex.message)
 +
 +
ex.message?.let { Log.e(TAG, it) }

2020年2月17日 (月) 16:33時点における最新版

androidstudioでjavaからkotlinへの変換

  1. MainActivity.javaを開いた状態で、codeの"Convert Java File to Kotlin File"を選択
  2. MainActivity.javaがMainActivity.ktへ変更され、コードもkotlinとなる
  3. GradleにKotlinを認識させるために、Tool/Kotlin/ConfigureKotlinInProjectを選択

"Assignments are not expressions, and only expressions are allowed in this context"エラーの時

var bytesRead: Int
while ((bytesRead = dataInputStream.read(buffer)) != -1) {
    outputStream.write(buffer, 0, bytesRead)
}

var bytesRead: Int
do {
    bytesRead = dataInputStream.read(buffer)
    if (bytesRead == -1)
        break
    outputStream.write(buffer, 0, bytesRead)
} while (true)

参考:https://discuss.kotlinlang.org/t/assignment-not-allow-in-while-expression/339

"Platform declaration clash: The following declarations have the same JVM signature"エラー

publicプロパティはsetterが自動でつき、名前がかぶるため。

class HogeClass {
    companion object {
        var name = ""
        fun setName(name: String) {
            this.name = name
        }
        fun getName(): String {
            return this.name
        }
    }
}

class HogeClass {
    companion object {
        private var name = ""
        fun setName(name: String) {
            this.name = name
        }
        fun getName(): String {
            return this.name
        }
    }
}

プロパティにprivateをつければ良い。

"type mismatch prequired string found string?"エラーの場合

Log.e(TAG, ex.message)

ex.message?.let { Log.e(TAG, it) }