gpt4 book ai didi

android - 原因 : couldn't make a guess for com. example.newmvvm.ListViewModel.listViewModel

转载 作者:行者123 更新时间:2023-12-05 00:13:44 24 4
gpt4 key购买 nike

我正在尝试一个简单的程序(带有编辑框和一个按钮)来理解 Android 中的 MVVM 和数据绑定(bind)功能。当我在 activity_main.xml 中添加字段时出现错误。我已重命名包和类名,但出现错误“原因:无法猜测 com.example.newmvvm.ListViewModel.listViewModel”。

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

已更新

MainActivity :

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
}

}

数据库:

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

Run Log :

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'.

最佳答案

问题出在com.example.newmvvm.ListViewModel.listViewModel

我猜这是你的包名 ListViewModel,它包含这个类 listViewModel

databinding 中,包名称应为小写,class 名称应以大写开头。否则将找不到。

用这个 listviewmodel 重命名这个包名 ListViewModel

并用这个 ListViewModel 重命名你的 listViewModel Observable 类。

你的类型看起来像这样。

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

添加并删除此行。listViewModel 不是 viewmodel 它是 Observable 类。

listViewModel viewModel = ViewModelProviders.of(this).get(listViewModel.class);

关于android - 原因 : couldn't make a guess for com. example.newmvvm.ListViewModel.listViewModel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59606548/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com