【Android】 双方向バインディングで『No resource type specified (at 'text' with value '@={obj.value}').』が出るときの対処

プログラミング 2016年10月1日

AndroidStudio2.1から、DataBindingにて待望の双方向バインディングが簡単に実装可能になりましたね。



双方向バインディング(2-Way Binding)

従来のViewModel -> Viewは以下のような記述でした。

<EditText
    android:id="@+id/editText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@{info.name}"
    android:textAppearance="?android:attr/textAppearanceMedium"/>

これを、以下ように記述するだけでViewModel <-> Viewが実現します。

<EditText
    android:id="@+id/editText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@={info.name}"
    android:textAppearance="?android:attr/textAppearanceMedium"/>

android:text="@={info.name}"のように=を挿入するだけです。



ビルドエラーの対処

ところが、上記の記述だとNo resource type specified (at 'text' with value '@={obj.value}').というエラーでビルドが通らない場合があります。


これは、以下のように設定ファイルを編集すると直る可能性があります。

// 以下をコメントアウト
// classpath "com.android.databinding:dataBinder:1.0-rc4"
// 以下をコメントアウト
// apply plugin: 'com.android.databinding'

// 以下を追加
android {
    ...
    dataBinding.enabled = true
}

色々試しましたが、自分はこれでいけました。ハマっている人は参考までに。



まとめ

バグ?

slont

金融ベンチャーでWebエンジニア

Great! You've successfully subscribed.
Great! Next, complete checkout for full access.
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.