Search code examples
javaandroidonclickoverridingsuperclass

JAVA android Studio method does not overridefrom its superclass


I am a beginner in Java development. I would like to know why I have the error

"method does not overridefrom its superclass" on the @override declaration.

Here is my code

package fr.cif.cif_app;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

public class DashboardActivity extends Activity {
    ImageView img = null;
    TextView monTexte = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dashboard);

        img = (ImageView) findViewById(R.id.imageView8);
        img.setOnClickListener((OnClickListener) this);
    }
    @Override
    public void onClick(View v) {
    /* Réagir au clic */

        // On récupère l'identifiant de la vue, et en fonction de cet identifiant…
        switch(v.getId()) {

            // Si l'identifiant de la vue est celui du premier bouton
            case R.id.imageView5:
    /* Agir pour bouton 1 */
                break;

            // Si l'identifiant de la vue est celui du deuxième bouton
            case R.id.imageView6:
    /* Agir pour bouton 2 */
                break;

            // Si l'identifiant de la vue est celui du deuxième bouton
            case R.id.imageView7:
    /* Agir pour bouton 2 */
                break;



    /* etc. */
        }

    }
}

I have read some other posts which was talking about some errors typing with the name of the function onClick ...but I don't see where is my error.

Thanks in advance for your help.


Solution

  • yes, you got 2 errors:

    1. Class cast Exception:img.setOnClickListener((OnClickListener) this);. At this line, you tried to cast your Activity as an OnClickListener but your actually your Activity is not. Hence, try to do implements OnclickListener with your activity.
    2. method does not overridefrom its superclass" on the @override declaration. --> that because you try to override the onClick() method from your father Activity class, but you can check your super class, there's no onClick method to override.

    Anws, in your case, just implements OnClickListener in your activity is enough.