The method onClick(View) of type oddg must override a superclass method?

可紊 提交于 2020-01-01 07:52:08

问题


I am occurring error like:

"The method onClick(View) of type oddg must override a superclass method".

I am confused where exactly error has occurred. Can you please guide me, what exactly error is?

public class oddg extends Activity implements OnClickListener
{
        ProgressDialog dialog;
        int increment;
        int maximum ;
        private static final String TAG = "ServicesDemo";

        @Override
        public void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main1);
            Button startbtn = (Button) findViewById(R.id.startbtn);
            startbtn.setOnClickListener(this);

        }

        @Override
        public void onClick(View arg0)
        { }
}

This is my code... Thanks in Advance-- Onkar


回答1:


I think the problem is that you're compiler settings are set to Java 1.5 instead of Java 1.6. Since Java 1.6 the annotation @Override is not only possible for inherited methods from a superclass but also for implemented methods from an interface. But if your compiler is still set to 1.5 the problem is that he will check if you're overwriting a method from a super class. In your example this is not the case since you're implementing a method from an interface.

As a side note it would be good to follow Java Naming Conventions in your code. So all classes begin with an upper case letter.




回答2:


I had the same trouble and I fixed it like this:

  • Open Project settings->Java Compiler
  • Set Enable Project Specific Settings to disable



回答3:


This isn't directly appropriate to this question but you will also get this error if you haven't declared implements OnClickListener on the class declaration.

// Wrong
public class aClass extends Activity {
}
// Right
public class aClass extends Activity implements OnClickListener {
}

This may seem dum to the more experienced coder but I'm new to this and it fooled me.




回答4:


Read about @Override annotation. It means, that once you have annotated the method with @Override, the compiler checks if it really an overrided method, and shows an error if its not.

Also, you have to have Language Level 6 in order to use it with interface implementing methods. In IDEA you can do it via Project Setup.




回答5:


I had the same problem. I found the solution writing well the import.

I replaced

import android.content.DialogInterface.OnClickListener; //(wrong)

for

import android.view.View.OnClickListener;



回答6:


"RoflcoptrException" is right! You shoud set java compiler to 1.6 Project properties -> Java compiler ->1.6




回答7:


If you are using android studio , you can also override your method by pressing ctrl+O or just go to code-> Overide and override appropriate method . It will automatically override your method .



来源:https://stackoverflow.com/questions/5776126/the-method-onclickview-of-type-oddg-must-override-a-superclass-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!