Activity cannot be converted to LifecycleOwner

痴心易碎 提交于 2020-01-03 16:52:52

问题


I would like to use Room with LiveData, and in other projects I already used it, but in this one, I can not get it to work. It can't convert my activity into Lifecycle activity when I try to observe the livedata, however, I'm using the AppCompatActivity, and I even tried to Override the getLifecycle method (which worked for me in previous projects). I even tried with AndroidX but still the same issue :(

Here my activity (Part of it):

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleRegistry;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

private LifecycleRegistry mLifecycleRegistry;

public class actMain extends AppCompatActivity  {

 @Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mLifecycleRegistry = new LifecycleRegistry(this);
    mLifecycleRegistry.markState(Lifecycle.State.CREATED);
}
  @Override
public void onStart() {
    super.onStart();
    EventBus.getDefault().register(this);
    //Firebase
    db = FirebaseFirestore.getInstance();

    mLifecycleRegistry.markState(Lifecycle.State.STARTED);

    alarmViewModel = ViewModelProviders.of(this).get(AlarmViewModel.class);

    alarmViewModel.getAlarmList().observe(actMain.class, new 
    Observer<List<Alarm>>() {
        @Override
        public void onChanged(@Nullable List<Alarm> alarms) {

        }
    });
}
@NonNull
@Override
public Lifecycle getLifecycle() {
    return mLifecycleRegistry;
}

Here is my gradle file:

implementation 'androidx.room:room-runtime:2.0.0-alpha1'
annotationProcessor 'androidx.room:room-compiler:2.0.0-alpha1'
implementation 'com.google.android.material:material:1.0.0-alpha3'
implementation  'androidx.lifecycle:lifecycle-viewmodel:2.0.0-alpha1'
implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0-alpha1'

And here is my Dao:

@Dao
public interface AlarmDao {

    @Query("SELECT * FROM alarm")
    LiveData<List<Alarm>> getAllAlarm();

    @Insert
    void insert(Alarm... alarms);

    @Update
    void update(Alarm... alarms);

    @Delete
    void delete(Alarm... alarms);

}

I tried every suggestion here including mine, but I can not figure out what is the issue in this case.

Edit: Code added


回答1:


You don't need to use

mLifecycleRegistry = new LifecycleRegistry(this);
mLifecycleRegistry.markState(Lifecycle.State.CREATED);

Since, new AppcompatActivity is already lifecyclerOwner.

You also observe class object, which is incorrect. actMain.class is a class object. You should have:

alarmViewModel.getAlarmList().observe(this, new Observer<List<Alarm>>() {
     @Override
     public void onChanged(@Nullable List<Alarm> alarms) {}
});



回答2:


Upgrade to latest version. Below is reference from my project AppCompatActivity now inherits from LifecycleOwner and you can straightaway implement the functionality you needed.

implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.core:core-ktx:1.0.1'
implementation 'com.google.android.material:material:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

implementation 'androidx.cardview:cardview:1.0.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.recyclerview:recyclerview:1.0.0'
implementation 'org.jetbrains.anko:anko:0.10.8'
implementation 'com.github.debop:koda-time:1.2.1'


implementation 'androidx.annotation:annotation:1.0.2'
implementation "androidx.legacy:legacy-support-core-utils:1.0.0"


// Room components
implementation "androidx.room:room-runtime:2.0.0"
annotationProcessor "androidx.room:room-compiler:2.0.0"

// Lifecycle components
implementation "androidx.lifecycle:lifecycle-runtime:2.0.0"
implementation "androidx.lifecycle:lifecycle-extensions:2.0.0"
annotationProcessor "androidx.lifecycle:lifecycle-compiler:2.0.0"

// Coroutines
implementation  "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.1.1"
implementation  "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.1.1"

Check for example here https://codelabs.developers.google.com/codelabs/android-room-with-a-view-kotlin/#13

Also you can refer the Basic example at https://github.com/googlesamples/android-architecture-components/tree/master/BasicSample



来源:https://stackoverflow.com/questions/50976622/activity-cannot-be-converted-to-lifecycleowner

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!