gpt4 book ai didi

java - 收到无法创建 Android ViewModel 类实例的错误

转载 作者:行者123 更新时间:2023-12-01 19:31:13 25 4
gpt4 key购买 nike

所以,我有这个 ViewModel,当我在 Debug模式下启动应用程序时,它说它无法创建该类的实例。我知道 View 模型的构造函数应该是空的,只有 super (应用程序)但我不知道在哪里放置存储库和字符列表。

public class CharacterViewModel extends AndroidViewModel{

private CharacterRepository characterRepository;
private LiveData<List<Personaje>> characters;
private boolean isUpdate = false;

public LiveData<List<Personaje>> getCharacters() {
return characters;
}


public CharacterViewModel(@NonNull Application application) {
super(application);
characterRepository = new CharacterRepository(application);
characters = characterRepository.getAll();
}



public boolean isUpdate(int uid) {
isUpdate = uid !=-1;
return isUpdate;
}
public LiveData<Personaje> getCharacterById(int idCharacter) {
return characterRepository.getCharactersById(idCharacter);
}
public void insertCharacter(Personaje personaje) {
if (isUpdate){
characterRepository.updateCharacter(personaje);
}
else{characterRepository.insert(personaje);}
}

}

如果需要的话,错误如下:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: cat.itb.RoomExample, PID: 10928
java.lang.RuntimeException: Cannot create an instance of class cat.itb.charactersheet.presentation.character.CharacterViewModel
at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:238)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:164)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:130)
at cat.itb.charactersheet.presentation.list.ListFragment.onActivityCreated(ListFragment.java:52)
at androidx.fragment.app.Fragment.performActivityCreated(Fragment.java:2619)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:904)
at androidx.fragment.app.FragmentManagerImpl.addAddedFragments(FragmentManagerImpl.java:2100)
at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1874)
at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1830)
at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1727)
at androidx.fragment.app.FragmentManagerImpl.dispatchStateChange(FragmentManagerImpl.java:2663)
at androidx.fragment.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManagerImpl.java:2613)
at androidx.fragment.app.Fragment.performActivityCreated(Fragment.java:2624)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:904)
at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1303)
at androidx.fragment.app.FragmentManagerImpl.dispatchStateChange(FragmentManagerImpl.java:2659)
at androidx.fragment.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManagerImpl.java:2613)
at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:246)
at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:542)
at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:178)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1425)
at android.app.Activity.performStart(Activity.java:7825)
at android.app.ActivityThread.handleStartActivity(ActivityThread.java:3294)
at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:221)
at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:201)
at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:173)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:230)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:164) 
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:130) 
at cat.itb.charactersheet.presentation.list.ListFragment.onActivityCreated(ListFragment.java:52) 
at androidx.fragment.app.Fragment.performActivityCreated(Fragment.java:2619) 
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:904) 
at androidx.fragment.app.FragmentManagerImpl.addAddedFragments(FragmentManagerImpl.java:2100) 
at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1874) 
at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1830) 
at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1727) 
at androidx.fragment.app.FragmentManagerImpl.dispatchStateChange(FragmentManagerImpl.java:2663) 
at androidx.fragment.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManagerImpl.java:2613) 
at androidx.fragment.app.Fragment.performActivityCreated(Fragment.java:2624) 
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:904) 
at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238) 
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1303) 
at androidx.fragment.app.FragmentManagerImpl.dispatchStateChange(FragmentManagerImpl.java:2659) 
at androidx.fragment.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManagerImpl.java:2613) 
at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:246) 
at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:542) 
at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:178) 
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1425) 
at android.app.Activity.performStart(Activity.java:7825) 
at android.app.ActivityThread.handleStartActivity(ActivityThread.java:3294) 
at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:221) 
at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:201) 
at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:173) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016) 
at android.os.Handler.dispatchMessage(Handler.java:107) 
at android.os.Looper.loop(Looper.java:214) 
at android.app.ActivityThread.main(ActivityThread.java:7356) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 
Caused by: java.lang.RuntimeException: cannot find implementation for cat.itb.charactersheet.presentation.database.CharacterAppDatabase. CharacterAppDatabase_Impl does not exist
at androidx.room.Room.getGeneratedImplementation(Room.java:94)
at androidx.room.RoomDatabase$Builder.build(RoomDatabase.java:952)
at cat.itb.charactersheet.presentation.database.CharacterAppDatabase.getDatabase(CharacterAppDatabase.java:16)
at cat.itb.charactersheet.presentation.database.CharacterRepository.<init>(CharacterRepository.java:19)
at cat.itb.charactersheet.presentation.character.CharacterViewModel.<init>(CharacterViewModel.java:28)
... 37 more

编辑:数据库在这里

package cat.itb.charactersheet.presentation.database;

import android.content.Context;

import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;


import cat.itb.charactersheet.presentation.model.Personaje;


public abstract class PersonajeAppDatabase extends RoomDatabase {
public abstract PersonajeDAO characterDAO();
private static volatile PersonajeAppDatabase INSTANCE;

public static PersonajeAppDatabase getDatabase(Context context){
if(INSTANCE == null){
synchronized (PersonajeAppDatabase.class){
if(INSTANCE == null){
INSTANCE = Room.databaseBuilder(context.getApplicationContext(), PersonajeAppDatabase.class, "character-database").build();
}
}
}
return INSTANCE;
}

public abstract PersonajeDAO getDAO();
}

必须说的是,我更改了一些类的名称,但“Character”等于“Personaje”,以防止进一步开发时出现混淆。

编辑2:这是成绩构建

apply plugin: 'com.android.application'
apply plugin: 'androidx.navigation.safeargs'

android {
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion

defaultConfig {
applicationId "cat.itb.RoomExample"
minSdkVersion 22
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'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

dependencies {

implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
implementation 'androidx.navigation:navigation-fragment:2.1.0'
implementation 'androidx.navigation:navigation-ui:2.1.0'
implementation 'androidx.recyclerview:recyclerview:1.0.0'
implementation 'com.jakewharton:butterknife:10.1.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:10.1.0'
def room_version = "2.2.0-rc01"
implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version"

}

最佳答案

这是真正的错误:由以下原因引起:java.lang.RuntimeException:找不到 cat.itb.charactersheet.presentation.database.CharacterAppDatabase 的实现。 CharacterAppDatabase_Impl 不存在

这意味着 Room 编译器应该生成的数据库类丢失了。这似乎是因为您的 PersonajeAppDatabase 类缺少 @Database 注释。请参阅https://developer.android.com/reference/androidx/room/Database.html有关如何添加此注释的更多详细信息。

关于java - 收到无法创建 Android ViewModel 类实例的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59257894/

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