Class 必须声明为抽象或实现抽象方法
Class must either be declared abstract or implement abstract method
我在 github 上找到了一个迷你 Deezer 播放器,但我认为它是用 Eclipse 编码的。然而,我在 Android 工作室工作。
作为我的第一个 Android 应用程序,它一定是一个菜鸟问题,但我坚持这个:
private DialogListener mDeezerDialogListener = new **DialogListener**() {
@Override
public void onComplete(Bundle values) {
// store the current authentication info
SessionStore sessionStore = new SessionStore();
sessionStore.save(mDeezerConnect, LoginActivity.this);
// Launch the Home activity
Intent intent = new Intent(LoginActivity.this, HomeActivity.class);
startActivity(intent);
}
@Override
public void onDeezerError(final DeezerError deezerError) {
Toast.makeText(LoginActivity.this, R.string.deezer_error_during_login,
Toast.LENGTH_LONG).show();
}
@Override
public void onError(final DialogError dialogError) {
Toast.makeText(LoginActivity.this, R.string.deezer_error_during_login,
Toast.LENGTH_LONG).show();
}
@Override
public void onCancel() {
Toast.makeText(LoginActivity.this, R.string.login_cancelled, Toast.LENGTH_LONG).show();
}
@Override
public void onOAuthException(OAuthException oAuthException) {
Toast.makeText(LoginActivity.this, R.string.invalid_credentials, Toast.LENGTH_LONG)
.show();
}
};`
bold 函数 给我一个错误,上面写着:
Class 'Anonymous class derived from DialogListener' must either be
declared abstract or implement abstract method
'onException(Exception)' in 'DialogListener'.
我不知道问题出在哪里,但雪上加霜的是,第一个@Override 一切正常,但第二个、第三个和最后一个给我这个错误:
Error:(91, 17) error: method does not override or implement a method
from a supertype
这应该是一个有效的代码片段,那么这里的问题是什么,为什么对某些 @Overrides 有抱怨?
这两个错误有关联吗?
编辑 1:
按照建议我添加了另一个函数:
@Override
public void onException(Exception exception) {
}
第一个错误消失了。 github 上的原始代码是为以前版本的 SDK 编写的,做的东西不一样吗?
@Override 错误保留。但据我所知,这些是 onException?
的子异常
编辑2:
这是在 Deezer SDK 中定义的:
import com.deezer.sdk.network.connect.event.DialogListener;
我正在查看他们的文档,它在 "Method Summary" 下提到:
onCancel()
,
onComplete(Bundle values)
,
onException(Exception exception)
。
它还说:
void onException(Exception exception)
在认证过程中抛出异常时调用。
可能会引发以下异常:
OAuthException
,
DeezerError
,
DialogError
。
我想我会post所有额外的数据。
编辑 3:
这是我重写代码的方式:
@Override
public void onException(Exception exception) {
if(exception instanceof DeezerError){
Toast.makeText(LoginActivity.this, R.string.deezer_error_during_login,
Toast.LENGTH_LONG).show();
}
else if(exception instanceof DialogError){
Toast.makeText(LoginActivity.this, R.string.deezer_error_during_login,
Toast.LENGTH_LONG).show();
}
else if(exception instanceof OAuthException){
Toast.makeText(LoginActivity.this, R.string.invalid_credentials, Toast.LENGTH_LONG)
.show();
}
else{
//not implemented?
}
}
给出警告:Condition 'exception instanceof OAuthException' is always 'false'
。
我将不得不解决这个问题,但现在这是一个不同的问题。
当您在 Java 中实现匿名抽象 classes 或接口时,您必须为所有抽象方法(在抽象 class 的情况下)或在中声明的所有方法提供实现接口。
您收到的错误通知您您没有实现 DialogListener
中声明的 onException
方法。
其他错误很可能与第一个错误有关,但也不一定是这样。通常,您犯的单个错误会触发多个编译器错误,在这种情况下,当您先清除一个错误时,其他错误也会被清除。当您确实遇到编译器错误时,您应该始终专注于清除第一个错误,因为其他错误可能是假错误而不是真正的问题。
"method does not override or implement a method from a supertype"
也可能意味着您声明的方法签名与抽象签名不匹配 class 并且在这种情况下 @Override
不被编译器接受。
正如您在评论中提到的,Deezer 文档说 DialogListener 有 3 种方法:
onCancel(), onComplete(Bundle values), onException(异常异常).
因此您只需实现这 3 个功能。
@Override
public void onComplete(Bundle values) {
// store the current authentication info
SessionStore sessionStore = new SessionStore();
sessionStore.save(mDeezerConnect, LoginActivity.this);
// Launch the Home activity
Intent intent = new Intent(LoginActivity.this, HomeActivity.class);
startActivity(intent);
}
@Override
public void onCancel() {
Toast.makeText(LoginActivity.this, R.string.login_cancelled, Toast.LENGTH_LONG).show();
}
@Override
public void onException(Exception e) {
// ...
}
并删除其他方法:onError 等。也许您的示例,如您所建议的,是针对另一个版本的 SDK。
注意:我不使用 Android Studio,但在 Eclipse 中您有一个自动创建所需方法的命令(空的,提到 'TODO')。也许 Android Studio 中也存在同样的情况?
我在 github 上找到了一个迷你 Deezer 播放器,但我认为它是用 Eclipse 编码的。然而,我在 Android 工作室工作。
作为我的第一个 Android 应用程序,它一定是一个菜鸟问题,但我坚持这个:
private DialogListener mDeezerDialogListener = new **DialogListener**() {
@Override
public void onComplete(Bundle values) {
// store the current authentication info
SessionStore sessionStore = new SessionStore();
sessionStore.save(mDeezerConnect, LoginActivity.this);
// Launch the Home activity
Intent intent = new Intent(LoginActivity.this, HomeActivity.class);
startActivity(intent);
}
@Override
public void onDeezerError(final DeezerError deezerError) {
Toast.makeText(LoginActivity.this, R.string.deezer_error_during_login,
Toast.LENGTH_LONG).show();
}
@Override
public void onError(final DialogError dialogError) {
Toast.makeText(LoginActivity.this, R.string.deezer_error_during_login,
Toast.LENGTH_LONG).show();
}
@Override
public void onCancel() {
Toast.makeText(LoginActivity.this, R.string.login_cancelled, Toast.LENGTH_LONG).show();
}
@Override
public void onOAuthException(OAuthException oAuthException) {
Toast.makeText(LoginActivity.this, R.string.invalid_credentials, Toast.LENGTH_LONG)
.show();
}
};`
bold 函数 给我一个错误,上面写着:
Class 'Anonymous class derived from DialogListener' must either be declared abstract or implement abstract method 'onException(Exception)' in 'DialogListener'.
我不知道问题出在哪里,但雪上加霜的是,第一个@Override 一切正常,但第二个、第三个和最后一个给我这个错误:
Error:(91, 17) error: method does not override or implement a method from a supertype
这应该是一个有效的代码片段,那么这里的问题是什么,为什么对某些 @Overrides 有抱怨?
这两个错误有关联吗?
编辑 1:
按照建议我添加了另一个函数:
@Override
public void onException(Exception exception) {
}
第一个错误消失了。 github 上的原始代码是为以前版本的 SDK 编写的,做的东西不一样吗?
@Override 错误保留。但据我所知,这些是 onException?
的子异常编辑2:
这是在 Deezer SDK 中定义的:
import com.deezer.sdk.network.connect.event.DialogListener;
我正在查看他们的文档,它在 "Method Summary" 下提到:
onCancel()
,
onComplete(Bundle values)
,
onException(Exception exception)
。
它还说:
void onException(Exception exception)
在认证过程中抛出异常时调用。
可能会引发以下异常:
OAuthException
,
DeezerError
,
DialogError
。
我想我会post所有额外的数据。
编辑 3:
这是我重写代码的方式:
@Override
public void onException(Exception exception) {
if(exception instanceof DeezerError){
Toast.makeText(LoginActivity.this, R.string.deezer_error_during_login,
Toast.LENGTH_LONG).show();
}
else if(exception instanceof DialogError){
Toast.makeText(LoginActivity.this, R.string.deezer_error_during_login,
Toast.LENGTH_LONG).show();
}
else if(exception instanceof OAuthException){
Toast.makeText(LoginActivity.this, R.string.invalid_credentials, Toast.LENGTH_LONG)
.show();
}
else{
//not implemented?
}
}
给出警告:Condition 'exception instanceof OAuthException' is always 'false'
。
我将不得不解决这个问题,但现在这是一个不同的问题。
当您在 Java 中实现匿名抽象 classes 或接口时,您必须为所有抽象方法(在抽象 class 的情况下)或在中声明的所有方法提供实现接口。
您收到的错误通知您您没有实现 DialogListener
中声明的 onException
方法。
其他错误很可能与第一个错误有关,但也不一定是这样。通常,您犯的单个错误会触发多个编译器错误,在这种情况下,当您先清除一个错误时,其他错误也会被清除。当您确实遇到编译器错误时,您应该始终专注于清除第一个错误,因为其他错误可能是假错误而不是真正的问题。
"method does not override or implement a method from a supertype"
也可能意味着您声明的方法签名与抽象签名不匹配 class 并且在这种情况下 @Override
不被编译器接受。
正如您在评论中提到的,Deezer 文档说 DialogListener 有 3 种方法: onCancel(), onComplete(Bundle values), onException(异常异常).
因此您只需实现这 3 个功能。
@Override
public void onComplete(Bundle values) {
// store the current authentication info
SessionStore sessionStore = new SessionStore();
sessionStore.save(mDeezerConnect, LoginActivity.this);
// Launch the Home activity
Intent intent = new Intent(LoginActivity.this, HomeActivity.class);
startActivity(intent);
}
@Override
public void onCancel() {
Toast.makeText(LoginActivity.this, R.string.login_cancelled, Toast.LENGTH_LONG).show();
}
@Override
public void onException(Exception e) {
// ...
}
并删除其他方法:onError 等。也许您的示例,如您所建议的,是针对另一个版本的 SDK。
注意:我不使用 Android Studio,但在 Eclipse 中您有一个自动创建所需方法的命令(空的,提到 'TODO')。也许 Android Studio 中也存在同样的情况?