I am trying to write a sample app using Android architecture components and but even after trying for days I could not get it to work. It gives me the above exception.
我正在尝试编写一个使用Android架构组件的示例应用程序,但即使在尝试了几天之后,我也无法让它正常工作。它给了我以上的例外。
Lifecycle owner:-
生命周期所有者:-
public class MainActivity extends LifecycleActivity {
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = findViewById(R.id.tv_user);
PostViewModel viewModel = ViewModelProviders.of(this).get(PostViewModel.class);
viewModel.loadPosts();
viewModel.getPost().observe(this, new Observer<Post>() {
@Override
public void onChanged(@Nullable Post post) {
if(post != null) {
textView.setText(post.toString());
}
}
});
}
}
ViewModel:-
视图模型:-
public class PostViewModel extends ViewModel {
private MediatorLiveData<Post> post;
private PostRepository postRepo;
PostViewModel() {
post = new MediatorLiveData<>();
postRepo = new PostRepository();
}
public LiveData<Post> loadPosts() {
post.addSource(postRepo.getPost(),
post -> this.post.setValue(post)
);
return post;
}
@NonNull
public LiveData<Post> getPost() {
return post;
}
}
更多回答
If you are using version 2.33-beta and upper remove these dependencies;
如果您使用的是2.33测试版及更高版本,请删除这些依赖项;
implementation "androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03"
kapt "androidx.hilt:hilt-compiler:1.0.0-beta01"
Keep only these two dependency
只保留这两个依赖项
implementation "com.google.dagger:hilt-android:2.33-beta"
kapt "com.google.dagger:hilt-android-compiler:2.33-beta"
Well this will fix the issue for sure
好吧,这肯定会解决这个问题
First of all make sure you have this dependency
首先,确保您具有这种依赖关系
//Room DB
implementation "androidx.room:room-runtime:2.2.5"
annotationProcessor 'android.arch.persistence.room:compiler:1.1.1'
Then remove viewmodel constructor, then create init function as your constructor
然后删除视图模型构造函数,然后创建init函数作为构造函数
public void init(Application application){
instance = new FirebaseDatabaseInstance();
databaseRepository = new DatabaseRepository(application);
allChatMembers = databaseRepository.getAllChatMembers();
}
Then this will solve...
那么这将会解决..。
Not for OP's case, but adding it here for newcomers.
不是为OP的案例,而是为新人添加的。
If you inherit from AndroidViewModel and there is some heavy work in the main Thread (like accessing the database) it'll wrongly throw this error.
如果继承自androidViewModel,并且在主线程中有一些繁重的工作(如访问数据库),它将错误地抛出此错误。
After I switched to inherit from ViewModel, it showed the correct error and I could move the heavy work to Dispatchers.IO
. After moving UI blocking stuff to Dispatchers.IO
, I re-tested using AndroidViewModel and everything works fine again.
在我切换到从ViewModel继承之后,它显示了正确的错误,我可以将繁重的工作转移到Dispatcher.IO。在将UI阻塞的东西转移到Dispatcher.IO之后,我重新使用androidViewModel进行了测试,一切又正常了。
Conclusion:
"UI blocking work on the main thread when inheriting from AndroidViewModel can throw this error".
结论:“从androidViewModel继承时,主线程上的UI阻塞会抛出这个错误”。
In case you are using Jepack Compose with a ViewModel
in your component.
The @Preview
annotation may cause this error. That was the problem in my case.
如果您正在使用Jepack Compose,请在组件中使用一个ViewModel。@Pview注释可能会导致此错误。这就是我的问题所在。
none of the above was my problem. I did added @HiltViewModel
and @AndroidEntryPoint
and @HiltAndroidApplication
like i had before and my code worked. the problem was that my project was multi-module and when i added the annotations it actually imported theme from other submodule but because the module didn't have those dependency in its own gradle.build it didn't compile, so i added these as same as other modules and it worked:
以上都不是我的问题。我确实像以前一样添加了@HiltViewModel和@AndroidEntryPoint以及@HiltAndroidApplication,我的代码工作了。问题是,我的项目是多模块的,当我添加注释时,它实际上从其他子模块导入了主题,但由于模块在自己的gradle.build中没有这些依赖关系,因此无法编译,所以我添加了与其他模块相同的注释,它工作了:
// Hilt
implementation("com.google.dagger:hilt-android:2.47")
kapt("com.google.dagger:hilt-compiler:2.44.2")
I'm using this example android-arcuitecture-component BasicSample to make a new project, facing a similar error log, found I did'n change de applicatio name
我正在使用这个示例Android-Arcuitecture-Component BasicSample来创建一个新项目,面对类似的错误日志,我发现我没有更改应用程序名称
AndroidManifest.xml
and that was my error, to fix put the aplicacion name to de BasicApp, this class is implement in the example.
这就是我的错误,为了修复将应用程序名称放到de BasicApp中,这个类在示例中实现。
...
<application
android:name=".BasicApp"
android:allowBackup="false"
更多回答
我是一名优秀的程序员,十分优秀!