代码之家  ›  专栏  ›  技术社区  ›  Angus

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

  •  0
  • Angus  · 技术社区  · 5 年前

    我试着用一个简单的程序(带有editbox和一个按钮)来理解Android中的MVVM和数据绑定特性。我在活动中添加字段时出错_main.xml文件.

    我已经更新了下面的代码。请帮助理解错误的原因。

    更新

    主要活动:

    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);
        }
    }
    

    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
        }
    
    }
    

    数据库:

    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
        }
    }
    

    活动_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'.
    
    1 回复  |  直到 5 年前
        1
  •  2
  •   Rahul sharma    5 年前

    问题就在这里 com.example.newmvvm.ListViewModel.listViewModel

    我猜这是你的包裹名称 ListViewModel 它包含了这个类 listViewModel

    databinding 包名称应为小写和小写 class

    重命名此包名称 ListView模型 listviewmodel

    并重新命名你的 ListView模型 ListView模型

    type="com.example.newmvvm.listViewModel.ListViewModel"
    

    添加并删除此行。 不是 viewmodel 它是 Observable 班级。

    listViewModel viewModel = ViewModelProviders.of(this).get(listViewModel.class);
    
        2
  •  2
  •   Hasan Khan    5 年前

    您没有为布局生成数据绑定对象,即

    MainActivityBinding binding  = DataBindingUtil.setContentView(this,R.layout.main_activity);
    

    现在在活动中使用viewmodel设置数据绑定

    Yourviewmodelclass  viewModel = ViewModelProviders.of(this).get(yourviewmodelclass.class);
    
    binding.SetviewModel(viewModel)