Home » Android » android – Class must either be declared abstract or implement abstract method

android – Class must either be declared abstract or implement abstract method

Posted by: admin June 16, 2020 Leave a comment

Questions:

I have found a mini Deezer player on github, but I think it’s been coded in Eclipse. I however work in Android Studio.

Beeing my first Android app it must be a noob question, but I am stuck on this:

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();
            }
        };`

The bold function is giving me an error which reads:

Class ‘Anonymous class derived from DialogListener’ must either be
declared abstract or implement abstract method
‘onException(Exception)’ in ‘DialogListener’.

I have no idea what is the problem, but to add insult to injury, all is well with the first @Override, but the second, the third and the last one give me this error:

Error:(91, 17) error: method does not override or implement a method
from a supertype

This is supposed to be a working code snippet, so what is the problem here, why the complaints on some of the @Overrides?

Are those two errors connected?

EDIT1:

As suggested I added another function:

@Override
            public void onException(Exception exception) {

            }

and the first error went away. Could it be that the original code on github was written for a previous version of SDK, that did stuff diferently?

@Override errors stayed. but as far as I can see, those are sub-exceptions of onException?

EDIT2:

This is defined in the Deezer SDK:

import com.deezer.sdk.network.connect.event.DialogListener; 

I am looking at their documentation and it mentions under “Method Summary”:
onCancel(),
onComplete(Bundle values),
onException(Exception exception).

It also says:
void onException(Exception exception) is called when an exception is thrown during the authentication process.

The following exceptions may be raised :
OAuthException,
DeezerError,
DialogError.

I think I will post all additional data here.

EDIT3:

This is how I rewrote the code:

@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?
        }

    }

Gives a warning: Condition 'exception instanceof OAuthException' is always 'false'.

I will have to work on that, but this is now a different question.

How to&Answers:

As you mention in your comment, the Deezer doc says that DialogListener has 3 methods:
onCancel(), onComplete(Bundle values), onException(Exception exception).

So you have to implements ONLY these 3 functions.

        @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) {
          //    ...
        }

And remove the other methods: onError, etc. Maybe you example, as you suggest, is for another version of the SDK.

Note: I don’t use Android Studio, but in Eclipse you have a command to automatically create needed methods (empty, with mention ‘TODO’). Maybe the same exists in Android Studio?

Answer:

When you implement anonymous abstract classes or interfaces in Java, you have to provide implementation for all abstract methods (in case of abstract class) or all methods declared in interface.

Error you are getting notifies you that you didn’t implement onException method that is declared in DialogListener.

Other errors are very likely connected to the first one, but that also doesn’t have to be the case. Usually, single error you make can trigger multiple compiler errors and in such case when you clear first one other also get cleared. When you do get compiler errors, you should always focus on clearing the first one because other may be fake errors and not real issues.

"method does not override or implement a method from a supertype" can also mean that signature of methods you have declared do not match to signatures in abstract class and in that case @Override is not accepted by compiler.