6
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

DataBindingで'Cannot resolve symbol'エラーが出てきた時の対処

Posted at

KotlinでDataBindingを使ってるときに公式ドキュメント通りに

<data>
    <import type="android.view.View"/>
</data>
android:visibility="@{user.isAdult ? View.VISIBLE : View.GONE}"

みたいな風に条件式使ってごにょごにょしようとしたら*Cannot resolve symbol [android:visibilityに記述した内容]*のエラーが出てきた

ちなみにKotlinでDataBindingするときはKotlinでAndroid Data Bindingを使ってみたが参考になります

調べてみた

android:visibility="@{View.GONE}"

を付けてみると……

> Cannot resolve symbol @{View.GONE}

もうこれKotlin関係ないんじゃ?となったのでぐーぐる大先生のお力を借りることに…

Issue 186266: 'Cannot resolve symbol' error on data binding expressions setting android:visibility attribute

さすがぐーぐる大先生って感じにIssueに上がってました

Follow http://developer.android.com/tools/data-binding/guide.html, using android:visibility instead of bind:visibility, but it shows "Cannot resolve symbol" link to android:visibility="@{user.isFriend ? View.VISIBLE : View.GONE}"

I know "bind:visibility" will fix this error, but could you please merge the "bind" functions into "android"?

とあるので

xmlns:bind="http://schemas.android.com/apk/res-auto"

をrootLayoutに付け足して

bind:visibility="@{user.isAdult ? View.VISIBLE : View.GONE}"

って感じにbind:visibilityで指定したら動きました!

まとめ

  • Cannot resolve symbolのエラーはKotlin環境特有のものではないかもしれないことがある
  • xmlでCannot resolve symbolエラーが出たらとりあえずbind:属性に変えてみよう

補足

ビルド失敗したらリビルドすればイケることがあります

以下環境

  • AndroidStudio1.5.1
  • com.android.tools.build:gradle:1.5.0
  • com.android.databinding:compiler:1.0-rc5
6
4
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?