AppCompatActivity 未实现 LifecycleOwner
2022-09-02 12:04:41
我正在使用安卓支持库 26.1.0。这些是应用程序模块中的依赖项 -
implementation "android.arch.lifecycle:runtime:1.0.0"
implementation "android.arch.lifecycle:extensions:1.0.0-beta1"
implementation "android.arch.persistence.room:rxjava2:1.0.0-beta1"
implementation "android.arch.lifecycle:common-java8:1.0.0-beta1"
annotationProcessor "android.arch.lifecycle:compiler:1.0.0-beta1"
但是当我尝试使用ViewModel时,如下所示 -
mUserViewModel.getUsers().observe(this,
(Observer<Resource<List<UserView>>>) listResource -> {
if(listResource != null){
this.handleDataState(listResource.status, listResource.data, listResource.message);
}
});
它在 处显示错误,错误消息为 。(虽然支持库版本是26.1 +,它已经实现了生命周期所有者)this
Wrong first argument type Found: packagename.BrowseActivity, required: android.arch.lifecycle.LifecycleOwner
我也尝试过实现我在此示例中发现的,这也不起作用。请提出一个解决方案,并让我知道我是否做错了什么。LifecycleRegistryOwner
编辑 1 - 这是活动类
import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ProgressBar;
import com.github.amitkma.boilerplate.app.R;
import com.github.amitkma.boilerplate.app.mapper.UserMapper;
import com.github.amitkma.boilerplate.app.model.UserModel;
import com.github.amitkma.boilerplate.app.ui.widget.EmptyView;
import com.github.amitkma.boilerplate.app.ui.widget.ErrorView;
import com.github.amitkma.boilerplate.presentation.data.Resource;
import com.github.amitkma.boilerplate.presentation.data.ResourceState;
import com.github.amitkma.boilerplate.presentation.factory.ViewModelFactory;
import com.github.amitkma.boilerplate.presentation.viewmodel.UserViewModel;
import com.github.amitkma.boilerplate.presentation.vo.UserView;
import java.util.ArrayList;
import java.util.List;
import javax.inject.Inject;
import dagger.android.AndroidInjection;
public class BrowseActivity extends AppCompatActivity {
@Inject
UserAdapter mUserAdapter;
@Inject
UserMapper mUserMapper;
@Inject
ViewModelFactory mViewModelFactory;
private UserViewModel mUserViewModel;
private RecyclerView mRecyclerView;
private ProgressBar mProgressBar;
private ErrorView mErrorView;
private EmptyView mEmptyView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidInjection.inject(this);
setContentView(R.layout.activity_browse);
mUserViewModel = ViewModelProviders.of(this, mViewModelFactory)
.get(UserViewModel.class);
mRecyclerView = findViewById(R.id.recycler_browse);
mProgressBar = findViewById(R.id.progress);
mEmptyView = findViewById(R.id.view_empty);
mErrorView = findViewById(R.id.view_error);
setupBrowseRecycler();
setupViewListeners();
}
@Override
protected void onStart() {
super.onStart();
mUserViewModel.getUsers().observe(this,
(Observer<Resource<List<UserView>>>) listResource -> {
if(listResource != null){
this.handleDataState(listResource.status, listResource.data, listResource.message);
}
});
}
....
}
这是视图模型类
import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.ViewModel;
import com.github.amitkma.boilerplate.domain.interactor.GetUserList;
import com.github.amitkma.boilerplate.domain.model.User;
import com.github.amitkma.boilerplate.presentation.data.Resource;
import com.github.amitkma.boilerplate.presentation.data.ResourceState;
import com.github.amitkma.boilerplate.presentation.mapper.UserMapper;
import com.github.amitkma.boilerplate.presentation.vo.UserView;
import java.util.ArrayList;
import java.util.List;
import javax.inject.Inject;
import io.reactivex.subscribers.DisposableSubscriber;
public class UserViewModel extends ViewModel {
private final GetUserList mGetUserList;
private final UserMapper mUserMapper;
@Inject
public UserViewModel(GetUserList getUserList,
UserMapper userMapper) {
mGetUserList = getUserList;
mUserMapper = userMapper;
fetchUsers();
}
public void fetchUsers(){
mUserMutableLiveData.postValue(new Resource(ResourceState.LOADING, null, null));
mGetUserList.execute(new UserSubscriber(), null);
}
public LiveData<Resource<List<UserView>>> getUsers(){
return mUserMutableLiveData;
}
@Override
protected void onCleared() {
mGetUserList.dispose();
super.onCleared();
}
private final MutableLiveData<Resource<List<UserView>>> mUserMutableLiveData =
new MutableLiveData<>();
}