Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class must either be declared abstract or implement abstract method

Tags:

android

deezer

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.

like image 217
iggy Avatar asked Jan 01 '15 18:01

iggy


People also ask

Is it necessary to implement all abstract methods of abstract class?

It's not necessary for an abstract class to have abstract method. We can mark a class as abstract even if it doesn't declare any abstract methods. If abstract class doesn't have any method implementation, its better to use interface because java doesn't support multiple class inheritance.

Are abstract methods mandatory to implement?

An abstract class is not required to have an abstract method in it. But any class that has an abstract method in it or that does not provide an implementation for any abstract methods declared in its superclasses must be declared as an abstract class.

Can you implement functions in an abstract class?

An abstract method doesn't have any implementation (method body). A class containing abstract methods should also be abstract. We cannot create objects of an abstract class. To implement features of an abstract class, we inherit subclasses from it and create objects of the subclass.

How do you give default implementation in the abstract method?

If you want to have default implementation of a method in your abstract class, you have to use non-abstract methods. In abstract classes, you can declare fields with or without static and final modifiers. And concrete methods can be not just public, but also default, protected or private.


1 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?

like image 64
Thierry Avatar answered Oct 05 '22 21:10

Thierry