DataBinding错误的编译类

时间:2018-09-30 16:57:06

标签: android data-binding kotlin android-databinding

我陷入了这个问题。一切都很好,但是DataBinding Code生成器在编写代码时出错。我写错了吗?如果你们遇到与我相同的问题并成功解决,请帮助我。这是我的问题: ActivityLoginBinding

我没有一个名为View的类,它只是一个包名。看这个:

Folder Structure

也许我的xml代码有问题,所以这里是:

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

    <data>

        <variable
            name="activity"
            type="galihlprakoso.com.surat_kaleng_app.View.LoginActivity"/>

    </data>
    <android.support.constraint.ConstraintLayout
        android:background="@drawable/main_bg"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="galihlprakoso.com.surat_kaleng_app.View.LoginActivity">


        <ImageView
            android:scaleType="centerCrop"
            android:src="@drawable/main_background"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <TextView
            android:text="Surat Kaleng"
            android:textSize="40sp"
            android:layout_marginBottom="20dp"
            app:layout_constraintBottom_toTopOf="@id/act_login_main_logo"
            android:textColor="@android:color/white"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            android:textAlignment="center"
            android:fontFamily="@font/ptmono"
            android:layout_width="0dp"
            android:layout_height="wrap_content" />

        <ImageView
            android:id="@+id/act_login_main_logo"
            android:src="@drawable/main_logo"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:layout_width="200dp"
            android:layout_height="200dp" />

        <Button
            android:onClick="@{()->activity.doLogin()}"
            android:text="@string/login_dengan_google"
            android:fontFamily="@font/ptmono"
            android:textColor="@android:color/white"
            android:background="@drawable/main_btn"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            android:layout_margin="20dp"
            android:layout_width="0dp"
            android:layout_height="wrap_content" />

        <TextView
            android:text="Menghubungkan orang terdekat secara misterius."
            app:layout_constraintTop_toBottomOf="@id/act_login_main_logo"
            android:textColor="@android:color/white"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            android:textAlignment="center"
            android:fontFamily="@font/ptmono"
            android:layout_width="0dp"
            android:layout_height="wrap_content" />

        <ImageView
            android:src="@drawable/login_top_menu"
            android:layout_margin="20dp"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            android:layout_width="50dp"
            android:layout_height="50dp" />

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

这是错误:

/Users/galihlarasprakoso/Projects/Android/suratkalengapp/app/build/generated/data_binding_base_class_source_out/debug/dataBindingGenBaseClassesDebug/out/galihlprakoso/com/surat_kaleng_app/databinding/ActivityLoginBinding.java:12: error: cannot find symbol
import galihlprakoso.com.surat_kaleng_app.View;
                                         ^
  symbol:   class View
  location: package galihlprakoso.com.surat_kaleng_app
/Users/galihlarasprakoso/Projects/Android/suratkalengapp/app/build/generated/data_binding_base_class_source_out/debug/dataBindingGenBaseClassesDebug/out/galihlprakoso/com/surat_kaleng_app/databinding/ActivityLoginBinding.java:19: error: package View does not exist
  protected View.LoginActivity mActivity;
                ^
/Users/galihlarasprakoso/Projects/Android/suratkalengapp/app/build/generated/data_binding_base_class_source_out/debug/dataBindingGenBaseClassesDebug/out/galihlprakoso/com/surat_kaleng_app/databinding/ActivityLoginBinding.java:27: error: package View does not exist
  public abstract void setActivity(@Nullable View.LoginActivity activity);
                                                 ^
/Users/galihlarasprakoso/Projects/Android/suratkalengapp/app/build/generated/data_binding_base_class_source_out/debug/dataBindingGenBaseClassesDebug/out/galihlprakoso/com/surat_kaleng_app/databinding/ActivityLoginBinding.java:30: error: package View does not exist
  public View.LoginActivity getActivity() {
             ^
4 errors

我该怎么办?

1 个答案:

答案 0 :(得分:0)

重命名(Shift + F6)查看程序包。您可以将其重命名为views / ui或其他任何名称。

<variable
   name="activity"
   type="galihlprakoso.com.surat_kaleng_app.views.LoginActivity"/>

始终遵循java naming conventions,建议您遵循。