开发者社区> 问答> 正文

原因:无法猜测com.example.newmvvm.ListViewModel.listViewM

我正在尝试一个简单的程序(带有编辑框和一个按钮),以了解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'. 

展开
收起
Puppet 2020-01-06 14:41:50 731 0
1 条回答
写回答
取消 提交回答
  • 您没有为布局生成数据绑定对象,即MainActivityBinding binding = DataBindingUtil.setContentView(this,R.layout.main_activity);

    现在使用binding.SetviewModel(您的viewmodel)设置数据绑定

    而您未设置的视图模型

    活动中

    
    Yourviewmodelclass viewModel = ViewModelProviders.of(this).get(yourviewmodelclass.class);
    
    2020-01-06 14:42:34
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载