Java @Override 必须覆盖或实现一个超类型

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

@Override must override or implement a supertype

javaandroideclipse

提问by user3070626

I am getting an error for @Override, and it is saying i must override or implement a supertype.

我收到一个错误@Override,它说我必须覆盖或实现一个超类型。

This is my code, and the bit that is giving me the error is protected void onNewIntent(Intent intent).

这是我的代码,给我错误的位是 protected void onNewIntent(Intent intent)。

Thanks

谢谢

 public class CartListActivity extends ListActivity {

        public class ProductListAdapter extends BaseAdapter {

            @Override
            protected void onNewIntent(Intent intent) {
                String query = "";

                if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
                    //use the query to search your data
                    query = intent.getStringExtra(SearchManager.QUERY);
                }
                int catId = intent.getIntExtra(MainActivity.SELECTED_CATEGORY, 0);
                if (catId != 0)
                    categoryId = catId;

                loader = new ListViewLoader(adapter, categoryId);
                if (query.length() > 0) {
                    loader.execute(String.format(MainActivity.WEBSERVER_GETLIST + "q=%s", categoryId, query));
                }
                else {
                    loader.execute(String.format(MainActivity.WEBSERVER_GETLIST, categoryId));
                }
            }

            private final Context context;
            private List<Product> itemList;
            public List<Product> getItemList() {
                return itemList;
            }

            public void setItemList(List<Product> itemList) {
                this.itemList = itemList;
            }

            public Context getContext() {
                return context;
            }

            public ProductListAdapter(Context c) {
                context = c;
            }

            @Override
            public int getCount() { 
                if(itemList == null) return 0;
                else return itemList.size();
            }       

            @Override
            public Object getItem(int position) { 
                if (itemList == null) return null;
                else return itemList.get(position);
            }

            @Override
            public long getItemId(int position) { 
                if (itemList == null) return 0;
                else return itemList.get(position).hashCode();
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {


                View cell = convertView;

                if (cell == null) {
                    // get layout from mobile xml
                    LayoutInflater inflater = ((Activity)context).getLayoutInflater();
                    cell = inflater.inflate(R.layout.adapter_product_list, parent, false);
                }

                Product p = itemList.get(position);

                //set value into textview according to position
                TextView textView = (TextView) cell.findViewById(R.id.product_title);
                textView.setText(p.getProductName());

                // add £ symbol
                textView = (TextView) cell.findViewById(R.id.product_info);
                textView.setText("Price: " + "£"+ p.getPrice());  

                //set value into imageview according to position
                ImageView imgView = (ImageView) cell.findViewById(R.id.product_image);
                // clear the image
                imgView.setImageDrawable(null);
                //and load from the network
                p.loadImage(imgView, 54, 54);   

                return cell;            

            }


        }

        public static final Integer[] productIcons = {
            0, // index 0 is empty
            R.drawable.books,
            R.drawable.films,
            R.drawable.music,
            R.drawable.games,
        };

        private int categoryId;
        private ProductListAdapter adapter;
        private ListViewLoader loader;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            // get the category from the intent
            Intent intent = getIntent();
            categoryId = intent.getIntExtra(MainActivity.SELECTED_CATEGORY, 0);

            adapter = new ProductListAdapter(this);
            setListAdapter(adapter);

            // Show the Up button in the action bar.
            setupActionBar();

            loader = new ListViewLoader(adapter, categoryId);
            loader.execute(String.format(MainActivity.WEBSERVER_GETLIST, categoryId));
        }

        /**
         * Set up the {@link android.app.ActionBar}.
         */
        private void setupActionBar() {

            getActionBar().setDisplayHomeAsUpEnabled(true);

        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.product_list, menu);
            return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
            case R.id.show_cart:
                //create the intent for the cart activity
                Intent intent = new Intent(getApplicationContext(), CartActivity.class);
                startActivity(intent);
                return true;
            }
            return super.onOptionsItemSelected(item);
        }

        @Override
        public void onListItemClick(ListView l, View v, int position, long id) {
            //create an intent
            Intent intent = new Intent(this, ProductActivity.class);
            Product p = (Product)adapter.getItem(position);
            //specify the extra parameters we want to pass
            intent.putExtra(MainActivity.SELECTED_CATEGORY, p.getCategoryId());
            intent.putExtra(MainActivity.SELECTED_PRODUCTID, p.getProductId());
            intent.putExtra(MainActivity.SELECTED_PRODUCTNAME, p.getProductName());
            intent.putExtra(MainActivity.SELECTED_PRODUCTPRICE, p.getPrice());
            intent.putExtra(MainActivity.SELECTED_SUITABLEFORKIDS, p.getSuitableForKids());

            startActivity(intent);
        }
    }

采纳答案by SalGad

Method onNewIntentcan only be overridden in an Activity. You're extending it in your Adapter. Can you move the method to the Activity class and try it?

方法onNewIntent只能在Activity. 你在你的Adapter. 能不能把方法移到Activity类试试?

回答by koljaTM

The @Override annotation tells the compiler that you intend to override a method from a subclass (or interface). If you don't (e.g. because you changed the signature and no longer override), it will generate a warning. Did you change something about the signature of the method or copied/moved it from somewhere else?

@Override 批注告诉编译器您打算覆盖子类(或接口)中的方法。如果您不这样做(例如,因为您更改了签名并且不再覆盖),它将生成警告。您是否更改了该方法的签名或从其他地方复制/移动了它?

回答by dbw

onNewIntentis a method of Activityand can only be overridden in a class which extendsActivity. You're extending it in your ProductListAdapterextending Adapter.

onNewIntent是 的方法,Activity并且只能在extendsActivity. 你在你的ProductListAdapter扩展中扩展它Adapter

Please, move the code to your upper class CartListActivityin which you have declared ProductListAdapterclass

请将代码移至您CartListActivity已声明ProductListAdapter班级的上层班级

public class CartListActivity extends ListActivity 
{
    @Override
    protected void onNewIntent(Intent intent) 
    {
        String query = "";
        if (Intent.ACTION_SEARCH.equals(intent.getAction()))
        {
            //use the query to search your data
            query = intent.getStringExtra(SearchManager.QUERY);
        }
        int catId = intent.getIntExtra(MainActivity.SELECTED_CATEGORY, 0);
        if (catId != 0)
            categoryId = catId;

        loader = new ListViewLoader(adapter, categoryId);
        if (query.length() > 0) 
        {
            loader.execute(String.format(MainActivity.WEBSERVER_GETLIST + "q=%s", categoryId, query));
        }
        else 
        {
            loader.execute(String.format(MainActivity.WEBSERVER_GETLIST, categoryId));
        }
    }

    //ALL OTHER FUNCTION OF CartListActivity

    public class ProductListAdapter extends BaseAdapter 
    {
        @Override
        public int getCount() 
        { 
            if(itemList == null) 
                return 0;
            else 
                return itemList.size();
        }       

        @Override
        public Object getItem(int position) 
        { 
            if (itemList == null) 
                return null;
            else
                return itemList.get(position);
        }

        @Override
        public long getItemId(int position) 
        { 
            if (itemList == null) 
                return 0;
            else 
                return itemList.get(position).hashCode();
        }
    }
}

回答by Manitoba

Your project is configured to use JRE source level 1.4 (or lower). To solve this problem in Eclipse

您的项目配置为使用 JRE 源代码级别 1.4(或更低)。在Eclipse中解决这个问题

  1. Go to the Propertiesof the Java project in Eclipse
  2. Go to the Java Compilermenu
  3. Check that the Compiler Compliance Levelis set to 1.5 or higher
  1. 转到PropertiesEclipse 中的 Java 项目
  2. 进入Java Compiler菜单
  3. 检查Compiler Compliance Level是否设置为 1.5 或更高

Just want to let you know that the @Overrideannotation is not required to override a method.

只是想让您知道@Override不需要注释来覆盖方法。