ошибка, когда привязка данных в android не может найти символ ActivityMainBindingImpl - PullRequest
0 голосов
/ 05 февраля 2020

Итак, я пытаюсь создать слушателя onclick на кнопке, используя привязку данных, используя этот учебник.

учебник привязки данных , так же как и когда я держу нажатие внутри моей активности. xml раскладка при нажатии кнопки работает. Но я хочу отправить свои данные в include_main_layout. xml, но он говорит мне, что у меня ошибка ..

build.gradle (app)

 apply plugin: 'com.android.application'

android {
    compileSdkVersion 29

    dataBinding {
        enabled = true
    }


    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "com.example.datahelpsworld"
        minSdkVersion 23
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

MainActivity. java

public class MainActivity extends AppCompatActivity {

    User user;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);


        user = new User();
        user.setEmail("Ebola chebola chernobyl caronvirus");
        user.setName("dilo dilo bolo bolo");


        binding.setUser(user);


        MyClickHandlers handlers = new MyClickHandlers(this);
        binding.setHandlers(handlers);


    }


    public class MyClickHandlers {

        Context context;

        public MyClickHandlers(Context context) {
            this.context = context;
        }

        public void onFabClicked(View view) {

            user.setName("Ashutosh is awesome");
            user.setEmail("email address is magical and cannot be retrieved");
            Toast.makeText(getApplicationContext(), "FAB clicked!", Toast.LENGTH_SHORT).show();
        }

        public void onButtonClick() {
            Toast.makeText(getApplicationContext(), "Button clicked!", Toast.LENGTH_SHORT).show();
        }

        public void onButtonClickWithParam(View view, User user) {
            Toast.makeText(getApplicationContext(), "Button clicked! Name: " + user.getName(), Toast.LENGTH_SHORT).show();
        }

        public boolean onButtonLongPressed(View view) {
            Toast.makeText(getApplicationContext(), "Button long pressed!", Toast.LENGTH_SHORT).show();
            return false;
        }
    }

}

activity_main. xml

<?xml version="1.0" encoding="utf-8"?>

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

    <data>


        <variable
            name="handlers"
            type="com.example.datahelpsworld.MainActivity.MyClickHandlers" />

        <variable
            name="user"
            type="com.example.datahelpsworld.User" />
    </data>


    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        tools:context=".MainActivity">

        <include
            layout="@layout/main_layout"
            bind:handlers="@{handlers}"
            bind:user="@{user}" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:longClickable="true"

            android:onClick="@{(v)->handlers.onFabClicked()}"
            android:text="activity main layout">

        </Button>

    </LinearLayout>


</layout>

main_layout. xml

<layout>

    <data>

        <variable
            name="user"
            type="com.example.datahelpsworld.User" />


              <variable
            name="handlers"
            type="com.example.datahelpsworld.MainActivity.MyClickHandlers" />


    </data>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:id="@+id/username"

            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@{user.name}">

        </TextView>

        <TextView
            android:id="@+id/email"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@{user.email}">

        </TextView>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="@={(v)->handlers.onButtonClick()}"
            android:text="inside include">

        </Button>



    </LinearLayout>
</layout>

моя ошибка в следующих строках ..

error: cannot find symbol class ActivityMainBindingImpl

Я пытался очистить проект и перестроить его с основания, но все равно не повезло. Что я делаю не так?

Ответы [ 2 ]

1 голос
/ 05 февраля 2020

Проблема здесь.

 android:onClick="@{(v)->handlers.onFabClicked()}"

это должно быть android:onClick="@{handlers.onFabClicked()}" просто.

0 голосов
/ 05 февраля 2020

Если вы хотите передать параметр просмотра из xml, тогда ваши onClick методы должны быть такими, как показано ниже.

android:onClick="@{(v)->handlers.onFabClicked(v)}"
...