Reputation: 136
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)"
Upvotes: 9
Views: 10876
Reputation: 3589
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta2'
also causing the issue downgrade into
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta1'
to avoid the error
Upvotes: 3
Reputation:
If you're finding this now, it might be because you added a pre-release version of androidx.recyclerview:recyclerview:1.1.0-*
, which apparently containes upstream androidx core
that didn't have Activity
implementing LifecycleOwner
.
Upvotes: 2
Reputation: 1977
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
Upvotes: 18