Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Activity cannot be cast to LifecycleOwner

My MainActivity extends AppCompatActivity

public class MainActivity extends AppCompatActivity {
private WebsiteViewModel websiteViewModel;

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


      setContentView(R.layout.activity_main);

        RecyclerView recyclerView = findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setHasFixedSize(true);

        final WebsiteAdapter adapter = new WebsiteAdapter();


        this.websiteViewModel = ViewModelProviders.of(this).get(WebsiteViewModel.class);

        this.websiteViewModel.getAllWebsites().observe(this, new Observer<List<Website>>() {
            @Override
            public void onChanged(@Nullable List<Website> websites) {
                adapter.setWebsites(websites);
            }
        });
        recyclerView.setAdapter(adapter);
    }
}

The error I am facing is "error: incompatible types: MainActivity cannot be converted to LifecycleOwner."

The WebsiteViewModel is

    public class WebsiteViewModel extends AndroidViewModel {
    private WebsiteRepository repository;
    private LiveData<List<Website>> allWebsites;

    public WebsiteViewModel(@NonNull Application application) {
        super(application);
        repository = new WebsiteRepository(application);
        allWebsites = repository.getAllWebsites();
    }

    public void insert(Website website) {
        repository.insert(website);
    }

    public void update(Website website) {
        repository.update(website);
    }

    public void delete(Website website) {
        repository.delete(website);
    }

    public void deleteAllWebsites() {
        repository.deleteAllWebsites();
    }

    public LiveData<List<Website>> getAllWebsites() {
        return allWebsites;
    }
}

In my other project, I am using a Fragment rather than an Activity to cast to a lifecycle owner, and that project runs without this error. However I am copying from https://codinginflow.com/tutorials/android/room-viewmodel-livedata-recyclerview-mvvm/part-6-recyclerview-adapter, but I dont know why I am having this issue. I using androidx

If I add implements LifecycleOwner in MainActivity then I get the error " java.lang.NoSuchMethodError: No super method getLifecycle()Landroidx/lifecycle/Lifecycle; in class Landroidx/core/app/ComponentActivity; or its super classes (declaration of 'androidx.core.app.ComponentActivity' appears in /data/app/com.example.project-2/split_lib_dependencies_apk.apk)"

like image 722
Alex Manbeck Avatar asked Feb 03 '19 04:02

Alex Manbeck


People also ask

What is LifecycleOwner?

ProcessLifecycleOwner. Class that provides lifecycle for the whole application process. A class that has an Android lifecycle. These events can be used by custom components to handle lifecycle changes without implementing any code inside the Activity or the Fragment.

How many types of LifecycleOwner available in fragment when and which LifecycleOwner should pass?

When using a ViewModel exposing LiveData s for a Fragment , there are currently two LifecycleOwners that can be used to oberve: the Fragment itself or. the Fragment 's property mViewLifecycleOwner .

Is activity a lifecycle owner?

A lifecycle owner is a component that has a lifecycle, like an activity or a fragment.

How do I find the owner of a life cycle?

You can just use another signature to get the LifecycleOwner like: public class MyLifecycleObserver implements LifecycleObserver { @OnLifecycleEvent(Lifecycle. Event. ON_START) public void onStartListener(LifecycleOwner owner){ ... } }


1 Answers

Try updating your AppCompat dependency to the most recent one. I updated from 'androidx.appcompat:appcompat:1.0.0-alpha1' to 'androidx.appcompat:appcompat:1.1.0-alpha03' and it fixed it

like image 81
Idee Avatar answered Sep 18 '22 07:09

Idee