我尝试创建一个youtube教程的视图模型,它应该显示数据库中的所有条目(但此时它应该只显示Toast,因为还没有Recycler视图(
打开活动时,我得到以下异常:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tutorial/com.example.tutorial.Activity2}: java.lang.RuntimeException: Cannot create an instance of class com.example.tutorial.EventViewModel
at com.example.tutorial.Activity2.onCreate(Activity2.java:23)
类看起来如下:
public class Activity2 extends AppCompatActivity {
private EventViewModel eventViewModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
eventViewModel = new ViewModelProvider(this).get(EventViewModel.class);
eventViewModel.getAllEvents().observe(this, new Observer<List<Event>>() {
@Override
public void onChanged(List<Event> events) {
Toast.makeText(Activity2.this, "thhthth", Toast.LENGTH_SHORT).show();
}
});
}
}
ViewModel类:
public class EventViewModel extends AndroidViewModel {
private EventRepository repository;
private LiveData<List<Event>> allEvents;
public EventViewModel(@NonNull Application application) {
super(application);
repository = new EventRepository(application);
allEvents = repository.getAllEvents();
}
public void insert(Event event) {
repository.insert(event);
}
public void update(Event event) {
repository.update(event);
}
public void delete(Event event) {
repository.delete(event);
}
public LiveData<List<Event>> getAllEvents() {
return allEvents;
}
}
我该怎么解决这个问题?(仍然是Android初学者btw:^(
提前感谢!
这会很好用:
1.转到你的build.gradle(模块:应用程序(并将其添加到依赖项中:
`implementation "android.arch.lifecycle:extensions:$lifecycle_version"`
确保在依赖项中包含def lifecycle_version = "2.2.0"
所以应该是这样的:
dependencies {
def lifecycle_version = "2.2.0" //make sure to have this
//Some implementations ...
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation "android.arch.lifecycle:extensions:$lifecycle_version" // make sure to have this too.
}
2.转到你的活动(在你的情况下是Activity2
(,并写下这行:
eventViewModel = new ViewModelProvider(this, ViewModelProvider.AndroidViewModelFactory.getInstance(this.getApplication())).get(EventViewModel.class);
所以你的代码应该是这样的:
public class Activity2 extends AppCompatActivity {
private EventViewModel eventViewModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
eventViewModel = new ViewModelProvider(this, ViewModelProvider.AndroidViewModelFactory.getInstance(this.getApplication())).get(EventViewModel.class);
eventViewModel.getAllEvents().observe(this, new Observer<List<Event>>() {
@Override
public void onChanged(List<Event> events) {
Toast.makeText(Activity2.this, "enjoy :)", Toast.LENGTH_SHORT).show();
}
});
}
}
3.运行你的应用程序。
您的viewModel扩展了AndroidViewModel。使用AndroidViewModelFactory:
yourViewModel = new ViewModelProvider.AndroidViewModelFactory(getApplication()).create(YourViewModel.class);
您必须在方法onCreate中定义"eventViewModel",并且不要生成通用变量。
public class Activity2 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
EventViewModel eventViewModel = new ViewModelProvider(this).get(EventViewModel.class);
...
}
}
因为"ViewModelProviders"已被弃用,所以对于java程序员来说,请使用以下内容:100%可以工作
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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) {
Toast.makeText(MainActivity.this, "Onchange ", Toast.LENGTH_SHORT).show();
}
});
转到您的build.gradle(模块:应用程序(
def lifecycle_version = "2.2.0"
def room_version = "2.2.5"
implementation "androidx.lifecycle:lifecycle-viewmodel:$lifecycle_version"
// LiveData
implementation "androidx.lifecycle:lifecycle-livedata:$lifecycle_version"
annotationProcessor "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"
implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version"
https://stackoverflow.com/a/60857926/13614001
Android Studio-版本3.6.2
这对我有效。
确保在build.gradle(模块:应用程序(中包含依赖项以及代码更改。
更重要的是,在包含依赖项之后"进行等级同步"。