2018/03/29

DataBinding v2: NullPointerException

DataBinding v2にすると

     Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'void android.databinding.Observable.addOnPropertyChangedCallback(android.databinding.Observable$OnPropertyChangedCallback)' on a null object reference
        at android.databinding.BaseObservableField.<init>(BaseObservableField.java:16)
        at android.databinding.ObservableField.<init>(ObservableField.java:73)
        at ...

問題のコードが下記.

private val hoge = ObservableField<Foo?>(null)

これを, 次のように修正することで解決した.

private val hoge= ObservableField<Foo?>()
2018/03/28

Kotlin & StringFormatMatches lint

val foo = 1
context.getString(R.string.string_format, foo)

↑こういうコードだと, ↓こんなLintエラーが出る.

Errors found:
/xxx/src/xxx/Hoge.kt:100: Error: Wrong argument type for formatting argument '#1' in string_format: conversion is 'd', received <ErrorType> (argument #2 in method call) [StringFormatMatches]
    setText(context.getString(R.string.string_format, foo))

/xxx/src/main/res/values/strings.xml:100: Conflicting argument declaration here
val foo: Int = 1
context.getString(R.string.string_format, foo)

これだとOK.
静的解析で型推論できずにハマっているのかな?