gpt4 book ai didi

java - 不兼容类型 : MainActivity cannot be converted to LifecycleOwner

转载 作者:太空宇宙 更新时间:2023-11-03 13:41:27 26 4
gpt4 key购买 nike

MainActivity cannot be converted to LifecycleOwner我用它作为 LiveCycle 所有者,但它被拒绝了,我得到了一个错误,如图所示。我在 Api 25 上工作,我认为这个问题可能与这个版本有关这是关于我的 sdk 的信息

compileSdkVersion 25
buildToolsVersion '25.0.2'

这是我的代码:

private void retrieveTasks() {
Log.d(TAG, "Actively retrieving the tasks from the DataBase");
// Extract all this logic outside the Executor and remove the Executor
// Fix compile issue by wrapping the return type with LiveData
LiveData<List<TaskEntry>> tasks = mDb.taskDao().loadAllTasks();
// Observe tasks and move the logic from runOnUiThread to onChanged
tasks.observe(this, new Observer<List<TaskEntry>>() {
@Override
public void onChanged(@Nullable List<TaskEntry> taskEntries) {
Log.d(TAG, "Receiving database update from LiveData");
mAdapter.setTasks(taskEntries);
}
});
}

我将 LiveData 依赖项放入我的 Gradle

compile "android.arch.lifecycle:extensions:1.0.0"
annotationProcessor "android.arch.lifecycle:compiler:1.0.0"

如果有人知道问题的原因,请告诉我

最佳答案

Support Library 26.1.0及之后的Fragments和Activity已经默认实现了LifecycleOwner接口(interface)

但是在版本 25 中你需要实现 LifecycleOwner 接口(interface)例如

public class MyActivity extends Activity implements LifecycleOwner {
private LifecycleRegistry mLifecycleRegistry;

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

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

@Override
public void onStart() {
super.onStart();
mLifecycleRegistry.markState(Lifecycle.State.STARTED);
}

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

来源:Handling lifecycles with lifecycle-aware components

关于java - 不兼容类型 : MainActivity cannot be converted to LifecycleOwner,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51180915/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com