[JAVA] Check if you're using Databinding and it doesn't work

Note that the binding file was not automatically generated several times and I was addicted to it.

I misunderstood the generated file name

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 the layout tag, but it doesn't generate automatically

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.

id Doesn't create a corresponding View

<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!

A compilation error occurred in the layout file. I fixed the relevant part, but it is undone every time and the correction is not reflected

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?

Layout file does not complete

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.

When refactoring (renaming, etc.), I try to edit up to the strange part

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

Check if you're using Databinding and it doesn't work
Things to check when it doesn't work with proguard
It doesn't work if the Map key is an array
[Boostrap] Why doesn't it work? jQuery-chan? ~ Fighting long and long errors ~
I'm writing test code using RSpec, but it doesn't work
Check when nvidia-smi on Ubuntu doesn't work
[gradle] pathingJar doesn't work if classpath contains spaces
[Quarkus] A trap that doesn't work even if you copy and paste the GCP Pub / Sub sample as it is