【问题标题】:Can't Resolve AcitivityMainBinding for DataBinding无法解决 DataBinding 的 AcitivityMainBinding
【发布时间】:2020-08-16 14:02:38
【问题描述】:

我正在尝试使用 MVVM 架构模式,但是当我输入 ActivityMainBinding 时,建议菜单中没有出现任何内容

我尝试更改 XML 文件名并将其改回原来的名称,但没有任何反应

这是我的依赖

android {
compileSdkVersion 28

defaultConfig {
    applicationId "com.example.alphabet"
    minSdkVersion 16
    targetSdkVersion 28
    versionCode 1
    versionName "1.0"

    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard- rules.pro'
    }
}
dataBinding{
    enabled = true
}
}

dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

def lifecycle_version = "2.1.0"

// ViewModel and LiveData
implementation "android.arch.lifecycle:extensions:$lifecycle_version"


implementation 'io.reactivex.rxjava3:rxandroid:3.0.0'
// Because RxAndroid releases are few and far between, it is recommended you also
// explicitly depend on RxJava's latest version for bug fixes and new features.
// (see https://github.com/ReactiveX/RxJava/releases for latest 3.x.x version)
implementation 'io.reactivex.rxjava3:rxjava:3.0.0'
}

主要活动

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);

}
}

xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<data>

</data>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

.................................................. .....

【问题讨论】:

    标签: android mvvm android-architecture-components


    【解决方案1】:

    数据绑定的xml格式错误,应该是:

    <layout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto">
        <data>
            <variable
                name="viewmodel"
                type="com.myapp.data.ViewModel" />
        </data>
        <ConstraintLayout... /> <!-- UI layout's root element -->
    </layout>
    

    根据文档:https://developer.android.com/topic/libraries/data-binding 根元素应为 &lt;layout&gt; ... &lt;/layout&gt;

    转换为 xml 数据绑定布局的简单方法是在现有的非数据绑定布局中的标准根元素视图/视图组上按 ALT 键并选择“转换”选项。

    【讨论】:

      猜你喜欢
      • 2018-02-18
      • 2017-02-14
      • 1970-01-01
      • 2018-06-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-28
      • 2012-10-11
      • 1970-01-01
      相关资源
      最近更新 更多