java oddg 类型的方法 onClick(View) 必须覆盖超类方法吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5776126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 12:43:16  来源:igfitidea点击:

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

javaandroidoverriding

提问by Smith

I am occurring error like:

我正在发生错误,例如:

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

“oddg 类型的 onClick(View) 方法必须覆盖超类方法”。

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

这是我的代码......提前致谢 - Onkar

回答by RoflcoptrException

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.

我认为问题在于您将编译器设置设置为 Java 1.5 而不是 Java 1.6。从 Java 1.6 开始,@Override 注释不仅适用于从超类继承的方法,也适用于来自接口的实现方法。但是如果你的编译器仍然设置为 1.5,问题是他会检查你是否覆盖了一个超类的方法。在您的示例中,情况并非如此,因为您是从接口实现方法。

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

作为旁注,在您的代码中遵循Java 命名约定会很好。所以所有的类都以大写字母开头。

回答by Dmitry Nelepov

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

我遇到了同样的问题,我是这样解决的:

  • Open Project settings->Java Compiler
  • Set Enable Project Specific Settingsto disable
  • 打开项目设置->Java 编译器
  • 启用项目特定设置设置disable

回答by Kev Moor

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

这并不直接适用于这个问题,但如果您没有implements OnClickListener在类声明中声明,您也会收到此错误。

// 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.

对于更有经验的编码员来说,这似乎很愚蠢,但我对此并不陌生,它愚弄了我。

回答by Vladimir Ivanov

Read about @Overrideannotation. 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.

阅读@Override注释。这意味着,一旦你用 注释了方法@Override,编译器就会检查它是否真的是一个被覆盖的方法,如果不是,则显示一个错误。

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.

此外,您必须拥有语言级别 6 才能将其与接口实现方法一起使用。在 IDEA 中,您可以通过项目设置来完成。

回答by Pablo

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;

回答by Kushagra Sharma

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

如果您使用的是 android studio,您还可以通过按ctrl+O来覆盖您的方法,或者只需转到代码->覆盖并覆盖适当的方法。它会自动覆盖您的方法。

回答by jiangws88

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

“RoflcoptrException”是对的!您应该将 java compiler 设置为 1.6 Project properties -> Java compiler ->1.6