Ошибка: не удается найти ActivitySplashBinding

Я пытаюсь проверить, вошел ли пользователь в систему или нет. Если да, то покажите определенную группу просмотра, в противном случае покажите другую группу просмотра. Чтобы проверить, вошел ли пользователь в систему или нет, я извлекаю пользователя из общих настроек (во время входа в систему пользователь сохраняется в общих настройках). Позвольте мне показать мой код.

Модель представления всплеска

public class SplashViewModel extends ViewModel {
    public final String TAG = "SplashViewModel";
    private final String GREETING = "Hi ";

    public ObservableBoolean isLoggedIn = new ObservableBoolean();
    public ObservableField<String> userName = new ObservableField<>("");
    private Preference<User> preference;

    SplashViewModel(Preference preference) {
        this.preference = preference;
        isLoggedIn.set(false);
    }

    public void getCurrentUser() {
        preference.get(Constants.CURRENT_USER, User.class)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(user -> {
                    isLoggedIn.set(true);
                    userName.set(GREETING + user.name);
                }, throwable -> isLoggedIn.set(false));
    }
}

активность_всплеск

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        tools:context="com.sevenbits.android.mvvmsample.view.SplashActivity">

    <data>
        <variable name="splashViewModel"
                  type="com.sevenbits.android.mvvmsample.viewmodel.SplashViewModel"/>

    </data>

    <android.support.constraint.ConstraintLayout
        android:id="@+id/parent1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/splash_bg">

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/loggedin_layout"
            android:visibility="@{splashViewModel.isLoggedIn ? View.VISIBLE : View.GONE,default=gone}">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="32sp"
                android:text="@{splashViewModel.userName}"
                android:textColor="@color/white"
                android:gravity="center"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintVertical_bias="0.40"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="32sp"
                android:text="Welcome Back"
                android:textColor="@color/white"
                android:gravity="center"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintVertical_bias="0.50"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="24sp"
                android:text="Start a New Game >>"
                android:textColor="@color/white"
                android:gravity="center"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintVertical_bias="0.70"/>

        </android.support.constraint.ConstraintLayout>

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/loggedout_layout"
            android:visibility="@{splashViewModel.isLoggedIn ? View.GONE : View.VISIBLE}"
            >

            <Button android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/bg_button"
                android:gravity="center"
                android:text="Login"
                android:textColor="@color/white"
                android:textSize="18sp"
                android:id="@+id/login_button"
                android:paddingStart="40dp"
                android:paddingEnd="40dp"
                android:paddingTop="20dp"
                android:paddingBottom="20dp"
                app:layout_constraintBottom_toTopOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintVertical_bias="0.33"/>

            <Button android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/bg_button"
                android:gravity="center"
                android:text="Sign Up"
                android:textColor="@color/white"
                android:textSize="18sp"
                android:id="@+id/sign_up_button"
                android:paddingStart="40dp"
                android:paddingEnd="40dp"
                android:paddingTop="20dp"
                android:paddingBottom="20dp"
                app:layout_constraintBottom_toTopOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                />


            <Button android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/bg_button"
                android:gravity="center"
                android:text="View Scores"
                android:textColor="@color/white"
                android:textSize="18sp"
                android:id="@+id/view_scores_button"
                android:paddingStart="40dp"
                android:paddingEnd="40dp"
                android:paddingTop="20dp"
                android:paddingBottom="20dp"
                app:layout_constraintVertical_bias="0.66"
                app:layout_constraintBottom_toTopOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent" />


            <Button android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@android:color/transparent"
                android:textColor="@color/white"
                android:textAllCaps="false"
                android:textSize="18sp"
                android:id="@+id/guest_button"
                android:layout_marginBottom="20dp"
                android:text="Play As a Guest User"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"

                />

        </android.support.constraint.ConstraintLayout>

    </android.support.constraint.ConstraintLayout>
</layout>

Всплеск активности

public class SplashActivity extends AppCompatActivity {

    SplashViewModel splashViewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initDataBinding();
        setButtonClickListeners();
    }

    @Override
    protected void onStart() {
        super.onStart();
        splashViewModel.getCurrentUser();
    }

    private void initDataBinding() {
        ActivitySplashBinding activitySplashBinding = DataBindingUtil.setContentView(this, R.layout.activity_splash);
        SplashViewModelFactory splashViewModelFactory = Injection.provideSplashViewModelFactory(this);
        splashViewModel = ViewModelProviders.of(this, splashViewModelFactory).get(SplashViewModel.class);
        activitySplashBinding.setSplashViewModel(splashViewModel);
//        splashViewModel.getCurrentUser();
    }

    private void setButtonClickListeners() {
        findViewById(R.id.guest_button).setOnClickListener(v -> {
            startActivity(new Intent(SplashActivity.this, GameActivity.class));
            finish();
        });

        findViewById(R.id.sign_up_button).setOnClickListener(v -> startActivity(
                new Intent(SplashActivity.this, RegisterActivity.class)));

        findViewById(R.id.login_button).setOnClickListener(v -> startActivity(
                new Intent(SplashActivity.this, LoginActivity.class)));

        findViewById(R.id.view_scores_button).setOnClickListener(v -> startActivity(
                new Intent(SplashActivity.this, ScoresActivity.class)));
    }
}

Но когда я пытаюсь запустить, выдает следующую ошибку.

ошибка: не удается найти класс символов ActivitySplashBinding

Может ли кто-нибудь помочь мне с этим?


person Riddhi    schedule 13.04.2018    source источник
comment
Вы используете плагин Gradle 3.1.0 Canary 6?   -  person Raghunandan    schedule 13.04.2018
comment
@Raghunandan да, я использую gradle 3.1.0.   -  person Riddhi    schedule 13.04.2018
comment
у вас есть этот android.databinding.enableV2=true, как указано здесь разработчика. android.com/topic/libraries/data-binding/index.html?   -  person Raghunandan    schedule 13.04.2018
comment
@ Рагунандан, да. Это там. Я использовал привязку данных и в других видах деятельности. и он работал нормально.   -  person Riddhi    schedule 13.04.2018
comment
@Raghunandan Я добавил эту строку android.databinding.enableV2=true в свой gradle.properties. Сейчас у меня error: cannot find symbol class ActivitySplashBindingImpl   -  person Riddhi    schedule 13.04.2018
comment
stackoverflow.com/a/38178632/3395198   -  person IntelliJ Amiya    schedule 13.04.2018
comment
@IntelliJAmiya Вы имели в виду, что мне нужно заменить ActivitySplashBinding на ActivityMainBinding ??   -  person Riddhi    schedule 13.04.2018
comment
@IntelliJAmiya, это тоже не помогло в моем случае.   -  person Riddhi    schedule 13.04.2018
comment
@Riddhi попробуйте следующее предложение   -  person Raghunandan    schedule 13.04.2018


Ответы (1)


Если вы используете плагин Gradle 3.1.0 Canary 6, вам нужно добавить android.databinding.enableV2=true в файл gradle.properties.

ошибка: не удается найти класс символов ActivitySplashBindingImpl

Попробуйте и добавьте

  <data>
    <import type="android.view.View" />

Это упоминается в разделе LayoutDetails при импорте по адресу https://developer.android.com/topic/libraries/data-binding/index.html

person Raghunandan    schedule 13.04.2018
comment
Спасибо. Это сработало. Я забыл импортировать View . и спасибо, что напомнили добавить android.databinding.enableV2=true - person Riddhi; 13.04.2018