本文介绍了AppCompatActivity未实现LifecycleOwner的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Android支持库26.1.0。这些是app模块中的依赖项 -

I am using Android Support Library 26.1.0. These are the dependencies in app module -

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时 -

But when I am trying to use ViewModel like below -

mUserViewModel.getUsers().observe(this,
            (Observer<Resource<List<UserView>>>) listResource -> {
                if(listResource != null){
                    this.handleDataState(listResource.status, listResource.data, listResource.message);
                }
            });

它在显示错误和错误消息是错误的第一个参数类型Found:packagename.BrowseActivity,required:android.arch.lifecycle.LifecycleOwner 。 (虽然支持库版本是26.1+已经实现了LifecycleOwner)

It is showing error at this and the error message is Wrong first argument type Found: packagename.BrowseActivity, required: android.arch.lifecycle.LifecycleOwner. (Although support library version is 26.1+ which already implements LifecycleOwner)

我还试图实现 LifecycleRegistryOwner 我哪个发现于此,这也无效。请提出解决方案,如果我做错了,请告诉我。

I have also tried to implement LifecycleRegistryOwner which I found in this sample and this is also not working. Kindly suggest a solution and let me know if I am doing anything wrong.

编辑1 -
这是活动类

Edit 1 -This is Activity class

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);
                }
            });
    }
    ....
}

这是ViewModel Class

This is ViewModel Class

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<>();

}

推荐答案

您不再需要实现它了。

只需通过扩展LifecycleOwner的AppCompatActivity扩展您的活动。

Just extend your activity by AppCompatActivity which extends LifecycleOwner.

您忘了覆盖

private final LifecycleRegistry lifecycleRegistry = new LifecycleRegistry(this);

@Override
public LifecycleRegistry getLifecycle() {
    return lifecycleRegistry;
}

您至少需要Support-Lib

You need at least Support-Lib 26.1

片段相同。只需通过 LifecycleFragment 扩展你的片段,扩展 android.support.v4.app.Fragment

Same for fragments. Just extend your Fragment by LifecycleFragment which extends android.support.v4.app.Fragment

编辑:刚刚使用Kotlin重建了一个快速示例项目并且它有效。

Just rebuilt a quick sample project using Kotlin and it worked.

class MainActivity : AppCompatActivity(), HasSupportFragmentInjector, AnkoLogger {
    private val lifecycleRegistry by lazy { android.arch.lifecycle.LifecycleRegistry(this) }
    private val viewModel by lazy { ViewModelProviders.of(this, viewModelFactory).get(BridgesVm::class.java) }
    override fun getLifecycle() = lifecycleRegistry

    public override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        viewModel.data.observe(this, Observer<Bridge?> { info { "Received" } })
   }
}

class BridgesVm @Inject constructor() : ViewModel() {
        val data: LiveData<Bridge> = MutableLiveData<Bridge>()
}

编辑:

使用arch版beta1时出现问题。在修复此问题之前,请考虑使用1.0.0-alpha9-1。在我的情况下,如果我使用beta1但使用alpha 9-1

There's a problem using arch version beta1. Consider using 1.0.0-alpha9-1 until this is fixed. In my case it wasnt working if i use beta1 but working with alpha 9-1

这篇关于AppCompatActivity未实现LifecycleOwner的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-18 09:03