Class must either be declared abstract or implement abstract method
Asked Answered
C

2

9

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.

Cleon answered 1/1, 2015 at 18:56 Comment(2)
Can you show us the DialogListener class (or interface) definition? And yes the 2 errors seem connected.Mesonephros
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'm missing somethin with the editor??Cleon
M
6

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?

Mesonephros answered 1/1, 2015 at 20:24 Comment(5)
I wouldn't know, I have been programing in Android studio for 6h. Will google it... thanks!Cleon
@Cleon hope it can help. Please accept the answer if it helps.Mesonephros
Both of you guys came up with the same solution at approx. the same time. How can I credit you both?Cleon
Dang, decided to accept your answer, but can not vote for Dalija. sorry Dalija, hopefully I will eventually return the favor somehow, you both helped.Cleon
@Cleon Thanks! Good choice ;-)Mesonephros
I
2

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.

Infantine answered 1/1, 2015 at 19:37 Comment(2)
Thanks, not there yet, but moving along. See original post.Cleon
I think your last edit is getting nearer to my problem... thanks!Cleon

© 2022 - 2024 — McMap. All rights reserved.