无法创建viewmodel类的示例

hk8txs48  于 2021-07-09  发布在  Java
关注(0)|答案(0)|浏览(377)

当我尝试运行我的应用程序时,它会因为这个logcat错误而崩溃。这个问题非常类似于这篇文章,但所有提到的解决方案都不适合我。
当我搜索这个错误时,我发现这通常是因为viewmodelproviders过时了,人们建议在factory类中使用新的viewmodelprovider,但即使这样也不适合我。 java.lang.RuntimeException: Cannot create an instance of class com.emirhalici.notetakingappnvvmexample.NoteViewModel 我不知道哪里会有问题,如果我有一个小的语法错误或其他东西。这让我发疯了,因为我是一个初学者,正在努力学习android架构组件。我在关注一个链接到这里的youtube播放列表,当他运行它时,他的代码运行得完美无瑕。请先看一下,然后再标记为复制品。我在这里真是太不懂了。提前谢谢。
logcat错误

Process: com.emirhalici.notetakingappnvvmexample, PID: 17577
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.emirhalici.notetakingappnvvmexample/com.emirhalici.notetakingappnvvmexample.MainActivity}: java.lang.RuntimeException: Cannot create an instance of class com.emirhalici.notetakingappnvvmexample.NoteViewModel
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:223)
    at android.app.ActivityThread.main(ActivityThread.java:7656)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
 Caused by: java.lang.RuntimeException: Cannot create an instance of class com.emirhalici.notetakingappnvvmexample.NoteViewModel
    at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:275)
    at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:187)
    at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:150)
    at com.emirhalici.notetakingappnvvmexample.MainActivity.onCreate(MainActivity.java:26)
    at android.app.Activity.performCreate(Activity.java:8000)
    at android.app.Activity.performCreate(Activity.java:7984)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) 
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:223) 
    at android.app.ActivityThread.main(ActivityThread.java:7656) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 
 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:267)
    at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:187) 
    at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:150) 
    at com.emirhalici.notetakingappnvvmexample.MainActivity.onCreate(MainActivity.java:26) 
    at android.app.Activity.performCreate(Activity.java:8000) 
    at android.app.Activity.performCreate(Activity.java:7984) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) 
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:223) 
    at android.app.ActivityThread.main(ActivityThread.java:7656) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 
 Caused by: java.lang.RuntimeException: cannot find implementation for com.emirhalici.notetakingappnvvmexample.NoteDatabase. NoteDatabase_Impl does not exist
    at androidx.room.Room.getGeneratedImplementation(Room.java:97)
    at androidx.room.RoomDatabase$Builder.build(RoomDatabase.java:1358)
    at com.emirhalici.notetakingappnvvmexample.NoteDatabase.getInstance(NoteDatabase.java:25)
    at com.emirhalici.notetakingappnvvmexample.NoteRepository.<init>(NoteRepository.java:15)
    at com.emirhalici.notetakingappnvvmexample.NoteViewModel.<init>(NoteViewModel.java:19)
    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:267) 
    at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:187) 
    at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:150) 
    at com.emirhalici.notetakingappnvvmexample.MainActivity.onCreate(MainActivity.java:26) 
    at android.app.Activity.performCreate(Activity.java:8000) 
    at android.app.Activity.performCreate(Activity.java:7984) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) 
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:223) 
    at android.app.ActivityThread.main(ActivityThread.java:7656) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 

主活动.java

public class MainActivity extends AppCompatActivity {
private NoteViewModel noteViewModel;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // these didn't work
    //noteViewModel = new ViewModelProvider(this, ViewModelProvider.AndroidViewModelFactory.getInstance(this.getApplication())).get(NoteViewModel.class);
    //noteViewModel = new ViewModelProvider(this).get(NoteViewModel.class);
    //noteViewModel = ViewModelProviders.of(this).get(NoteViewModel.class);
    // i tried this but this don't work either
    noteViewModel = new ViewModelProvider(this,
            ViewModelProvider.AndroidViewModelFactory.getInstance(this.getApplication())).get(NoteViewModel.class);
    noteViewModel.getAllNotes().observe(this, new Observer<List<Note>>() {
        @Override
        public void onChanged(List<Note> notes) {
            // update RecyclerView
            Toast.makeText(MainActivity.this, "onChanged", Toast.LENGTH_SHORT).show();
        }
    });
} }

note.java(实体类)

@Entity(tableName = "note_table")
public class Note {

@PrimaryKey(autoGenerate = true)
private int id;

private String title;
private String description;
private int priority;

// getters
public int getId() {
    return id;
}

public String getTitle() {
    return title;
}

public String getDescription() {
    return description;
}

public int getPriority() {
    return priority;
}

// id setter
public void setId(int id) {
    this.id = id;
}

// constructor
public Note(String title, String description, int priority) {
    this.title = title;
    this.description = description;
    this.priority = priority;
}

}

注解.java

@Dao
public interface NoteDao {

@Insert
void insert(Note note);

@Update
void update(Note note);

@Delete
void delete(Note note);

@Query("DELETE FROM note_table")
void deleteAll();

@Query("SELECT * FROM note_table ORDER BY priority DESC")
LiveData<List<Note>> getAllNotes();

}

注解数据库.java

@Database(entities = Note.class, version = 1)
public abstract class NoteDatabase extends RoomDatabase {

private static NoteDatabase instance;

public abstract NoteDao noteDao();

public static synchronized NoteDatabase getInstance(Context context) {
    if (instance == null) {
        instance = Room.databaseBuilder(context.getApplicationContext(),
                NoteDatabase.class, "note_database")
                .fallbackToDestructiveMigration()
                .addCallback(roomCallback)
                .build();
    }
    return instance;
}

private static RoomDatabase.Callback roomCallback = new RoomDatabase.Callback() {
    @Override
    public void onCreate(@NonNull SupportSQLiteDatabase db) {
        super.onCreate(db);
        new PopulateDbAsyncTask(instance).execute();
    }
};

private static class PopulateDbAsyncTask extends AsyncTask<Void, Void, Void> {
    private NoteDao noteDao;
    private PopulateDbAsyncTask(NoteDatabase db) {
        noteDao = db.noteDao();
    }

    @Override
    protected Void doInBackground(Void... voids) {
        noteDao.insert(new Note("Title 1","Description 1", 1));
        noteDao.insert(new Note("Title 2","Description 2", 2));
        noteDao.insert(new Note("Title 3","Description 3", 3));
        noteDao.insert(new Note("Title 4","Description 4", 1));
        noteDao.insert(new Note("Title 5","Description 5", 2));
        noteDao.insert(new Note("Title 6","Description 6", 4));
        return null;
    }
}
}

noterepository.java文件

public class NoteRepository {
private NoteDao noteDao;
private LiveData<List<Note>> allNotes;

public NoteRepository(Application application) {
    NoteDatabase database = NoteDatabase.getInstance(application);
    noteDao = database.noteDao();
    allNotes = noteDao.getAllNotes();
}

public void insert(Note note) {
    new InsertNoteAsyncTask(noteDao).execute(note);
}

public void update(Note note) {
    new UpdateNoteAsyncTask(noteDao).execute(note);
}

public void delete(Note note) {
    new DeleteNoteAsyncTask(noteDao).execute(note);
}

public void deleteAll() {
    new DeleteAllNotesAsyncTask(noteDao).execute();
}

public LiveData<List<Note>> getAllNotes() {
    return allNotes;
}

private static class InsertNoteAsyncTask extends AsyncTask<Note, Void, Void> {
    private NoteDao noteDao;

    private InsertNoteAsyncTask(NoteDao noteDao) {
        this.noteDao = noteDao;
    }

    @Override
    protected Void doInBackground(Note... notes) {
        noteDao.insert(notes[0]);
        return null;
    }
}

private static class UpdateNoteAsyncTask extends AsyncTask<Note, Void, Void> {
    private NoteDao noteDao;

    private UpdateNoteAsyncTask(NoteDao noteDao) {
        this.noteDao = noteDao;
    }

    @Override
    protected Void doInBackground(Note... notes) {
        noteDao.update(notes[0]);
        return null;
    }
}

private static class DeleteNoteAsyncTask extends AsyncTask<Note, Void, Void> {
    private NoteDao noteDao;

    private DeleteNoteAsyncTask(NoteDao noteDao) {
        this.noteDao = noteDao;
    }

    @Override
    protected Void doInBackground(Note... notes) {
        noteDao.delete(notes[0]);
        return null;
    }
}

private static class DeleteAllNotesAsyncTask extends AsyncTask<Note, Void, Void> {
    private NoteDao noteDao;

    private DeleteAllNotesAsyncTask(NoteDao noteDao) {
        this.noteDao = noteDao;
    }

    @Override
    protected Void doInBackground(Note... voids) {
        noteDao.deleteAll();
        return null;
    }
}
}

noteviewmodel.java文件

public class NoteViewModel extends AndroidViewModel {
private NoteRepository repository;
private LiveData<List<Note>> allNotes;

public NoteViewModel(@NonNull Application application) {
    super(application);
    repository = new NoteRepository(application);
    allNotes = repository.getAllNotes();
}

public void insert(Note note) {
    repository.insert(note);
}

public void update(Note note) {
    repository.update(note);
}

public void delete(Note note) {
    repository.delete(note);
}

public void deleteAllNotes() {
    repository.deleteAll();
}

public LiveData<List<Note>> getAllNotes() {
    return allNotes;
}

}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题