错误:找不到ActivitySplashBinding [英] Error: Cannot find ActivitySplashBinding

查看:103
本文介绍了错误:找不到ActivitySplashBinding的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检查用户是否登录.如果是,则显示一个特定的视图组,否则显示另一个视图组.为了检查用户是否已登录,我正在从共享首选项中获取用户(在登录时,用户已保存为共享首选项).让我展示我的代码.

I am trying to check that if user is logged on or not. If yes then show a specific view group otherwise show different view group. To check whether user is logged on or not I am fetching the user from shared preference (at the time of login user is saved in shared preference). Let me show my code.

SplashViewModel

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));
    }
}

activity_splash

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

SplashActivity

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)));
    }
}

但是当我尝试运行时,它给了我以下错误.

But when I try to run, it gives me the following error.

错误:找不到符号类ActivitySplashBinding

error: cannot find symbol class ActivitySplashBinding

有人可以帮我吗?

推荐答案

如果您正在使用gradle插件3.1.0 Canary 6,则需要在gradle.properties文件中添加android.databinding.enableV2=true

If you are using gradle plugin 3.1.0 Canary 6 you need to add android.databinding.enableV2=true in your gradle.properties file

错误:找不到符号类ActivitySplashBindingImpl

error: cannot find symbol class ActivitySplashBindingImpl

尝试添加

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

在导入下 https://developer.android.com/topic/libraries/data-binding/index.html

这篇关于错误:找不到ActivitySplashBinding的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆