java @override 方法不会覆盖或实现来自超类型的方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27463173/
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-11-02 11:50:25  来源:igfitidea点击:

@override method does not override or implement a method from a supertype

javaandroid

提问by Bcrscorpio

I'm having a small problem regarding my coding for android. I'm still very new to this and run into a small issue. No matter what I seem to do I cant seem to clear the error on my @override. The error is @override method does not override or implement a method from a supertype and the error is on all 3 of the last 3 @Override. Through that it comes up with an error for Menu, menu, MenuItem, itmsearch and KeyEvent saying cannot find symbol. Any help would be great and thank you in advance.

我在为 android 编码时遇到了一个小问题。我对此还是很陌生,遇到了一个小问题。无论我似乎做什么,我似乎都无法清除@override 上的错误。错误是@override 方法没有覆盖或实现来自超类型的方法,并且错误出现在最后 3 个 @Override 中的所有 3 个上。通过它,它出现了 Menu、menu、MenuItem、itmsearch 和 KeyEvent 的错误,说找不到符号。任何帮助都会很棒,并在此先感谢您。

import android.os.Bundle;
import android.widget.ArrayAdapter;

public class MainActivity extends ListActivity
{


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    super.setTitle("My Films");

    setContentView(R.layout.main);

    setListAdapter(new ArrayAdapter<String>(this,
    R.layout.film_list_cell,
    R.id.text,
    CELLS));

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main_menu, menu);
    return true;
}
@Override
public boolean onOptionsItemsSelected(MenuItem item) {
    if (item.getItemId() ==R.id.itmsearch) {
    onSearchRequested();
    return true;
    }
    return false;
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    if( keyCode == KeyEvent.KEYCODE_SEARCH) {
        onSearchRequested();
        return true;
    }
    return false;
}



static final String[] CELLS = 
        new String[] {"Cell 0", "Cell 1", "Cell 2",
            "Cell 3", "Cell 4", "Cell 5",
            "Cell 6", "Cell 7", "Cell 8", 
            "Cell 9", "CEll 10"};
        }

回答by Eran

Change onOptionsItemsSelectedto onOptionsItemSelected.
................................................^

更改onOptionsItemsSelectedonOptionsItemSelected
......................................................^

Here's the correct name of the method you want to override :

这是您要覆盖的方法的正确名称:

public boolean onOptionsItemSelected (MenuItem item)

public boolean onOptionsItemSelected(MenuItem 项)

EDIT:

编辑:

Through that it comes up with an error for Menu, menu, MenuItem, itmsearch and KeyEvent saying cannot find symbol.

通过它,它出现了 Menu、menu、MenuItem、itmsearch 和 KeyEvent 的错误,说找不到符号。

Make sure you import all the relevant classes :

确保导入所有相关类:

import android.view.Menu;
import android.view.KeyEvent;
import android.view.MenuItem;