【DataBinding】ビルド時に「Could not find accessor XXXXX」というエラーが表示される原因

やりたいこと

DataBindingViewModelを使用しているときに、ビルドすると以下のようなエラーが表示されるのを修正したい。

  • Could not find accessor XXXXX
  • e: [kapt] An exception occurred: android.databinding.tool.util.LoggedErrorException: Found data binding errors.

手順

ViewModelのObservableFieldにprivateがついていないかどうか確認しましょう。
privateがついているとXMLから見えなくなります。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
class MainViewModel : ViewModel() { private val timeText: ObservableField<String> by lazy { // privateつけるとXMLから見えなくなる ObservableField<String>() } fun onClick() { timeText.set("time: ${System.currentTimeMillis()}") }
}
class MainViewModel : ViewModel() { private val timeText: ObservableField<String> by lazy { // privateつけるとXMLから見えなくなる ObservableField<String>() } fun onClick() { timeText.set("time: ${System.currentTimeMillis()}") } }
class MainViewModel : ViewModel() { private val timeText: ObservableField<String> by lazy { // privateつけるとXMLから見えなくなる ObservableField<String>() } fun onClick() { timeText.set("time: ${System.currentTimeMillis()}") }
}

ソースコード

MainActivity.kt

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package katano.kota.databindingpractice
import android.databinding.DataBindingUtil
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import katano.kota.databindingpractice.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() { private val viewModel: MainViewModel by lazy { MainViewModel() } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val binding: ActivityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main) binding.viewmodel = viewModel }
}
package katano.kota.databindingpractice import android.databinding.DataBindingUtil import android.support.v7.app.AppCompatActivity import android.os.Bundle import katano.kota.databindingpractice.databinding.ActivityMainBinding class MainActivity : AppCompatActivity() { private val viewModel: MainViewModel by lazy { MainViewModel() } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val binding: ActivityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main) binding.viewmodel = viewModel } }
package katano.kota.databindingpractice
import android.databinding.DataBindingUtil
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import katano.kota.databindingpractice.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() { private val viewModel: MainViewModel by lazy { MainViewModel() } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val binding: ActivityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main) binding.viewmodel = viewModel }
}

 

MainViewModel.kt

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package katano.kota.databindingpractice
import android.arch.lifecycle.ViewModel
import android.databinding.ObservableField
class MainViewModel : ViewModel() { val timeText: ObservableField<String> by lazy { ObservableField<String>() } fun onClick() { timeText.set("time: ${System.currentTimeMillis()}") }
}
package katano.kota.databindingpractice import android.arch.lifecycle.ViewModel import android.databinding.ObservableField class MainViewModel : ViewModel() { val timeText: ObservableField<String> by lazy { ObservableField<String>() } fun onClick() { timeText.set("time: ${System.currentTimeMillis()}") } }
package katano.kota.databindingpractice
import android.arch.lifecycle.ViewModel
import android.databinding.ObservableField
class MainViewModel : ViewModel() { val timeText: ObservableField<String> by lazy { ObservableField<String>() } fun onClick() { timeText.set("time: ${System.currentTimeMillis()}") }
}

 

activity_main.xml

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"> <data> <variable name="viewmodel" type="katano.kota.databindingpractice.MainViewModel" /> </data> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <android.support.v7.widget.AppCompatButton android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Button" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="8dp" android:text="@{viewmodel.timeText}"/> </LinearLayout>
</layout>
<?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android"> <data> <variable name="viewmodel" type="katano.kota.databindingpractice.MainViewModel" /> </data> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <android.support.v7.widget.AppCompatButton android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Button" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="8dp" android:text="@{viewmodel.timeText}"/> </LinearLayout> </layout>
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"> <data> <variable name="viewmodel" type="katano.kota.databindingpractice.MainViewModel" /> </data> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <android.support.v7.widget.AppCompatButton android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Button" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="8dp" android:text="@{viewmodel.timeText}"/> </LinearLayout>
</layout>