Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/187.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 数据绑定在模块中不起作用_Android_Android Databinding - Fatal编程技术网

Android 数据绑定在模块中不起作用

Android 数据绑定在模块中不起作用,android,android-databinding,Android,Android Databinding,我正在应用程序中使用数据绑定。我有一个应用程序模块,数据绑定工作顺利 但是,我还有一个“主模块”,我尝试使用相同的技术,但是xml中的数据绑定出现以下错误: 错误:(63,34)未指定资源类型(在具有值的“onClick”处 “@{viewModel::onButtonClicked}”) 我发现这表明这是一个问题,但已经解决了 我看不出代码有任何问题,我认为问题在于它在库模块中 我能做些什么来让它工作吗 主页模块 activity_home.xml <?xml version="1.0"

我正在应用程序中使用数据绑定。我有一个应用程序模块,数据绑定工作顺利

但是,我还有一个“主模块”,我尝试使用相同的技术,但是xml中的数据绑定出现以下错误:

错误:(63,34)未指定资源类型(在具有值的“onClick”处 “@{viewModel::onButtonClicked}”)

我发现这表明这是一个问题,但已经解决了

我看不出代码有任何问题,我认为问题在于它在库模块中

我能做些什么来让它工作吗

主页模块

activity_home.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="viewModel" type="com.flowmellow.projectx.home.ui.viewmodel.HomeViewModel"/>
    </data>

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/home_screen"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:context="com.flowmellow.projectx.home.ui.viewmodel.HomeActivity">

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/AppTheme.PopupOverlay"/>

        </android.support.design.widget.AppBarLayout>

        <android.support.constraint.ConstraintLayout
            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"
            android:id="@+id/content_main"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            tools:context="com.flowmellow.projectx.home.ui.viewmodel.HomeActivity">

            <TextView
                android:text="@string/activity_home_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/activity_home_title"
                android:textAppearance="@style/TextAppearance.AppCompat.Large"
                android:layout_marginStart="16dp"
                app:layout_constraintLeft_toLeftOf="parent"
                android:layout_marginLeft="16dp"
                android:layout_marginEnd="16dp"
                app:layout_constraintRight_toRightOf="parent"
                android:layout_marginRight="16dp"
                app:layout_constraintBottom_toTopOf="@+id/activity_home_button"
                android:layout_marginBottom="64dp" />

            <Button
                android:text="@string/activity_home_button"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:id="@+id/activity_home_button"
                style="@style/Widget.AppCompat.Button.Colored"
                android:onClick="@{viewModel::onButtonClicked}"/>

        </android.support.constraint.ConstraintLayout>

    </android.support.design.widget.CoordinatorLayout>
</layout>
HomeViewModel

public class HomeViewModel {

    private CustomSensorManager mSensorManager;

    public HomeViewModel(@NonNull CustomSensorManager sensorManager) {
        mSensorManager = sensorManager;
    }

    public void onButtonClicked(View view) {
        mSensorManager.scan(true);
    }
}
格雷德尔先生

apply plugin: 'com.android.library'
android {
    compileSdkVersion 25
    buildToolsVersion "25.0.0"

    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    dataBinding {
        enabled = true
    }
}
dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')

    compile 'com.android.support:appcompat-v7:25.0.0'
    compile 'com.android.support:design:25.0.0'
    compile 'com.android.support.constraint:constraint-layout:+'
}
更新:

可能是个虫子

我的问题:


引擎模块没有UI,因此缺少依赖项,现在很有意义。

假设您的应用程序中有这样的多级模块层次结构:

为了将数据绑定到模块A,您必须确保在整个层次结构中启用从根模块到模块A的数据绑定

这意味着您必须添加:

dataBinding {
    enabled = true
}
各模块:

  • 应用程序模块
  • 模块A
  • 模块B

重要的是要记住,您在
模块B
中也启用了数据绑定,因此所有路径都被覆盖。

您在库和应用程序模块中都启用了数据绑定吗?是的,在应用程序模块中启用了我刚刚测试过这一点,它可以工作。我怀疑您在库模块中也没有启用数据绑定。100%我在应用程序和库模块中都启用了数据绑定。感谢您的测试。但如果这对你有用的话,我一定是做错了什么。。。我得再试一次。感谢您花费时间。仅供参考,库模块中的build.gradle已复制到问题中。是的!这是我的问题,我有另一个模块依赖于家庭模块。现在很好,谢谢。还需要这个吗?我面临一个问题,我有一个android模块,它的第三个子依赖项使用数据绑定。当我将数据绑定也添加到app gradle时,我得到了R8:Program type ready present异常,如果我将其从app模块中删除,则不会发生该异常。
dataBinding {
    enabled = true
}