Note that the binding file was not automatically generated several times and I was addicted to it.
MainActivityBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
The above code will result in a compilation error. The name of the Activity is MainActivity, but Databinding is generated according to the name of the layout file, so the name of the generated class will be ActivityMainBinding. I misunderstood it as MainActivityBinding and was quite addicted to it.
I surrounded it with a layout tag like this, but it is not automatically generated
<layout>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/my_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title" />
</layout>
This is because the xmlns: android description part has become TextView. If you place this in the correct place, it will be automatically generated.
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="@+id/my_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title" />
</layout>
Like this.
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="@id/my_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title" />
</layout>
Why isn't binding.myTextView supposed to be generated automatically? This is because the id is specified incorrectly. To be exact, it should be @ + id / my_text_view. In the case of findViewById, it works even if there is no +, but it seems that DataBinding does not work.
You can fix it to the correct format of @ + id as follows.
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="@+id/my_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title" />
</layout>
Like this!
If you often look at the tabs, are you opening and editing the automatically generated layout file?
The file that opens on build error is an automatically generated file, so you need to manually open and edit the original file. Isn't this a bug in Android Studio 2.3?
The contents of the data tag of the code below will be completed properly, but activity.getMyText will not.
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="activity"
type="ore.MainActivity"
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@{activity.getMyText}"
android:orientation="vertical">
</LinearLayout>
</layout>
You can only jump partially. At first I thought that this was a mistake in writing, but it seems to be a specification. This is too subtle, so I stopped writing onClick in the layout file.
This is probably a bug in the refactoring feature of Android Studio 2.3. It seems that it is necessary to recognize that the refactoring function cannot be used basically where Databinding is used, or that the refactoring function needs to be used with caution.
Recommended Posts