我正在尝试一个简单的程序(带有编辑框和一个按钮),以了解Android中的MVVM和数据绑定功能。我在activity_main.xml中添加字段时遇到错误。我已经重命名了程序包和类名,但是却收到错误消息“原因:无法对com.example.newmvvm.ListViewModel.listViewModel进行猜测”。
我已经更新了下面的代码。请帮助理解错误原因。
更新
主要活动 :
package com.example.newmvvm.View;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.lifecycle.ViewModelProviders;
import android.os.Bundle;
import com.example.newmvvm.ListViewModel.listViewModel;
import com.example.newmvvm.R;
import com.example.newmvvm.databinding.ActivityMainBinding;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityMainBinding activityMainBinding = DataBindingUtil.setContentView(this,R.layout.activity_main);
activityMainBinding.setViewModel(new listViewModel());
listViewModel viewModel = ViewModelProviders.of(this).get(listViewModel.class);
}
}
listViewModel:
package com.example.newmvvm.ListViewModel;
import androidx.databinding.BaseObservable;
import androidx.lifecycle.ViewModel;
import com.example.newmvvm.Model.DB;
public class listViewModel extends BaseObservable {
DB db;
public listViewModel(){
db = new db("");
}
public String setTextMsg(String text){
db.setTextMsgModel(text);
}
public String getTextMsg(){
return db.getTextMsgModel();
}
public void onClick(){
//TODO
}
}
D B :
package com.example.newmvvm.Model;
public class DB {
private String textMsgModel;
public DB(String text){
this.textMsgModel = text;
}
public String setTextMsgModel(String text){
this.textMsgModel = text;
}
public String getTextMsgModel(){
return this.textMsgModel;
}
public void onClickModel(){
//TODO
}
}
activity_main.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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".View.MainActivity">
<data>
<variable
name="viewModel"
type="com.example.newmvvm.ListViewModel.listViewModel" />
</data>
<LinearLayout>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{ViewModel.setTextMsg}"
tools:layout_editor_absoluteX="33dp"
tools:layout_editor_absoluteY="28dp" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me"
tools:layout_editor_absoluteX="21dp"
tools:layout_editor_absoluteY="67dp" />
</LinearLayout>
</layout>
gradle.build:
apply plugin: 'com.android.application'
android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "com.example.newmvvm"
minSdkVersion 26
targetSdkVersion 29
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.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'android.arch.lifecycle:extensions:1.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}
运行日志:
9:09:36 PM: Executing task 'build'...
Executing tasks: [build] in project
C:\Users\rbeat\AndroidStudioProjects\NewMVVM\app
> Task :app:preBuild UP-TO-DATE
> Task :app:preDebugBuild UP-TO-DATE
> Task :app:compileDebugAidl NO-SOURCE
> Task :app:checkDebugManifest UP-TO-DATE
> Task :app:compileDebugRenderscript NO-SOURCE
> Task :app:generateDebugBuildConfig UP-TO-DATE
> Task :app:prepareLintJar UP-TO-DATE
> Task :app:prepareLintJarForPublish UP-TO-DATE
> Task :app:generateDebugSources UP-TO-DATE
> Task :app:dataBindingExportBuildInfoDebug UP-TO-DATE
> Task :app:dataBindingExportFeaturePackageIdsDebug UP-TO-DATE
> Task :app:dataBindingMergeDependencyArtifactsDebug UP-TO-DATE
> Task :app:dataBindingMergeGenClassesDebug UP-TO-DATE
> Task :app:generateDebugResValues UP-TO-DATE
> Task :app:generateDebugResources UP-TO-DATE
> Task :app:mergeDebugResources UP-TO-DATE
> Task :app:dataBindingGenBaseClassesDebug FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:dataBindingGenBaseClassesDebug'.
> couldn't make a guess for com.example.newmvvm.ListViewModel.listViewModel
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 0s
11 actionable tasks: 1 executed, 10 up-to-date
9:09:38 PM: Task execution finished 'build'.
您没有为布局生成数据绑定对象,即MainActivityBinding binding = DataBindingUtil.setContentView(this,R.layout.main_activity);
现在使用binding.SetviewModel(您的viewmodel)设置数据绑定
而您未设置的视图模型
活动中
Yourviewmodelclass viewModel = ViewModelProviders.of(this).get(yourviewmodelclass.class);
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。