英文:
'AmbientLifecycleObserver' is abstract; cannot be instantiated
问题
谷歌更新了WearOS中环境模式的工作方式,但我在实施这些更改时遇到了问题。
这是他们所做的更改:
https://android-review.googlesource.com/c/platform/frameworks/support/+/2534423
> 将AmbientLifecycleObserverInterface重命名为AmbientLifecycleObserver
问题是我现在得到了这个错误消息:
> 'AmbientLifecycleObserver'是抽象的,无法实例化
我的代码看起来完全像他们提供的示例代码:
https://android-review.googlesource.com/c/platform/frameworks/support/+/2534423/4/wear/wear-samples-ambient/src/main/java/androidx/wear/samples/ambient/MainActivity.kt
所以我困惑于我做错了什么。这是`AmbientLifecycleObserver`的代码:
https://android-review.googlesource.com/c/platform/frameworks/support/+/2534423/4/wear/wear/src/main/java/androidx/wear/ambient/AmbientLifecycleObserver.kt
这是我的代码:
private AmbientLifecycleObserver mAmbientLifecycleObserver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//出现错误 'AmbientLifecycleObserver'是抽象的,无法实例化
mAmbientLifecycleObserver = new AmbientLifecycleObserver(this, mAmbientCallback);
getLifecycle().addObserver(mAmbientLifecycleObserver);
}
final AmbientLifecycleObserver.AmbientLifecycleCallback mAmbientCallback = new AmbientLifecycleObserver.AmbientLifecycleCallback() {
@Override
public void onEnterAmbient(@NonNull AmbientLifecycleObserver.AmbientDetails ambientDetails) {
}
@Override
public void onUpdateAmbient() {
}
@Override
public void onExitAmbient() {
}
};
英文:
Google updated how ambient mode works in WearOS but I'm having trouble implementing the changes.
This is the change they made:
https://android-review.googlesource.com/c/platform/frameworks/support/+/2534423
> Rename AmbientLifecycleObserverInterface -> AmbientLifecycleObserver
The problem is I'm now getting this error message:
> 'AmbientLifecycleObserver' is abstract; cannot be instantiated
My code looks exactly like the sample code they provide:
So I'm confused on what I'm doing wrong. This is the code for AmbientLifecycleObserver
:
This is my code:
private AmbientLifecycleObserver mAmbientLifecycleObserver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//getting error 'AmbientLifecycleObserver' is abstract; cannot be instantiated
mAmbientLifecycleObserver = new AmbientLifecycleObserver(this, mAmbientCallback);
getLifecycle().addObserver(mAmbientLifecycleObserver);
}
final AmbientLifecycleObserver.AmbientLifecycleCallback mAmbientCallback = new AmbientLifecycleObserver.AmbientLifecycleCallback() {
@Override
public void onEnterAmbient(@NonNull AmbientLifecycleObserver.AmbientDetails ambientDetails) {
}
@Override
public void onUpdateAmbient() {
}
@Override
public void onExitAmbient() {
}
};
答案1
得分: 1
Here's the translated content:
如果你看一下他们所做的实际更改,你在他们的代码中看到的AmbientLifecycleObserver
调用 不是 构造函数 - 它是一个顶级函数调用(例如,静态方法),只是碰巧返回接口AmbientLifecycleObserver
的实例。
因为他们的代码是Kotlin,调用顶级函数在外观上与构造函数非常相似。
但在Java代码中,你需要使用与静态方法相同的语法来调用该顶级函数,在这种情况下是在为每个文件创建的自动生成的类上,名为AmbientLifecycleObserverKt
:
mAmbientLifecycleObserver = AmbientLifecycleObserverKt.AmbientLifecycleObserver(
this, mAmbientCallback);
你实际上可以在API签名文件中看到生成的Java方法名称。
英文:
If you look at the actual change they made, the AmbientLifecycleObserver
call that you see in their code is not a constructor - it is a top level function call (e.g., a static method) that just happens to return an instance of the interface AmbientLifecycleObserver
.
Because their code is in Kotlin, calling a top level function looks just like a constructor.
In Java code though, you need to call that top level function using the same syntax as a static method, in this case on the automatically generated class that is created for each file, AmbientLifecycleObserverKt
:
mAmbientLifecycleObserver = AmbientLifecycleObserverKt.AmbientLifecycleObserver(
this, mAmbientCallback);
You can actually see the generated Java method name in the API signature file.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论