android包* .databinding不存在

时间:2017-04-05 09:00:29

标签: android data-binding android-databinding

我阅读了有关此问题的更多链接,但我不知道我的项目究竟是什么问题。我在dataBinding上将build.gradle设置为true,下面的代码是我的应用build.gradle文件内容

apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "com.text.myapp"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    dataBinding {
        enabled = true
    }
}

repositories {
    jcenter()
    maven { url "https://jitpack.io" }
    maven { url "https://clojars.org/repo/" }
}

def support_library = "25.2.0"

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile "com.android.support:appcompat-v7:${support_library}"
    compile "com.android.support:support-v13:${support_library}"
    compile "com.android.support:cardview-v7:${support_library}"
    compile "com.android.support:recyclerview-v7:${support_library}"
}

我的活动:

public class ActivityRegister extends AppCompatActivity {

    private ActivityRegisterBinding binding;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_register);
    }
}

然后,活动布局

<?xml version="1.0" encoding="utf-8"?>
<layout
    xmlns:android="http://schemas.android.com/apk/res/android">

    <data>
        <variable
            name="presenter"
            type="ir.pishguy.cafealachiqpro.Ui.Register.ActivityMain.Model.ActivityRegisterViewModel"/>
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="-5dp"
        android:orientation="vertical">

        <TextView
            android:id="@+id/default_port"
            android:layout_width="match_parent"
            android:visibility="gone"
            android:layout_height="@dimen/default_textview_height"/>
    </LinearLayout>
</layout>

Logcat出错:

/Users/mahdi/Desktop/Home/Projects/Android/CafeAlachiqPro/app/src/main/java/com/text/myapp/Ui/Register/ActivityMain/Presenter/ActivityRegister.java
Error:(11, 45) error: package com.text.myapp.databinding does not exist
Error:(19, 13) error: cannot find symbol class ActivityRegisterBinding
Warning:The following options were not recognized by any processor: 
'[android.databinding.artifactType, android.databinding.printEncodedErrors, android.databinding.minApi, android.databinding.isTestVariant, android.databinding.enableDebugLogs, android.databinding.sdkDir, android.databinding.bindingBuildFolder, android.databinding.enableForTests, android.databinding.modulePackage, android.databinding.generationalFileOutDir, android.databinding.xmlOutDir]'
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.

我的viewModel:

public class ActivityRegisterViewModel extends BaseObservable {
    private String readContactPermission;
    private String getMessages;

    public ActivityRegisterViewModel() {
    }

    @Bindable
    public String getReadContactPermission() {
        return readContactPermission;
    }

    public void setReadContactPermission(String readContactPermission) {
        this.readContactPermission = readContactPermission;
        notifyChange();
    }

    public String getGetMessages() {
        return getMessages;
    }

    public void setGetMessages(String getMessages) {
        this.getMessages = getMessages;
    }
}

2 个答案:

答案 0 :(得分:1)

删除

申请插件:&#39; com.neenbedankt.android-apt&#39;

apt&#34; com.github.Raizlabs.DBFlow:dbflow-processor:$ {dbflow_version}&#34;

从gradle开始。这有助于我摆脱这个错误。

虽然我可以运行你的项目因为ir.pishguy.cafealachiqpro.Ui.Register.Robot.Model.RobotViewModel无法从robot_user_action.xml解析

<data class="UserMessagesDataBinding">

        <variable
            name="viewModel"
            type="ir.pishguy.cafealachiqpro.Ui.Register.Robot.Model.RobotViewModel">
        </variable>
    </data>

答案 1 :(得分:0)

请提及定义viewmodel的变量标签,如下所示:

    <?xml version="1.0" encoding="utf-8"?>
    <layout
        xmlns:android="http://schemas.android.com/apk/res/android">

        <data>

           <variable
              name="<variablename>"
              type="<packagename.YourViewModel>" />
        </data>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="-5dp"
            android:orientation="vertical">

            <TextView
                android:id="@+id/default_port"
                android:layout_width="match_parent"
                android:visibility="gone"
                android:layout_height="@dimen/default_textview_height"/>
        </LinearLayout>
    </layout>