2013-12-09 32 views
0

我得到一個錯誤@Override,它是說我必須重寫或實現一個超類型。@Override必須覆蓋或實現一個超類型

這是我的代碼,並且給我錯誤的位是protected void onNewIntent(意圖intent)。

由於

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); 
     } 
    } 

回答

0

OnNewIntent只能在活動中重寫。您正在將它擴展到適配器中。你可以將方法移動到Activity類並嘗試它嗎?

+0

我試過了,錯誤消失了,但是我試圖實現的搜索功能並沒有回退任何結果。謝謝 – user3070626

+0

將公共方法添加到適配器並從活動中的NewIntent方法傳輸結果:)如果標記幫助您,請將其標記爲正確 – SalGad

0

的@Override註解告訴你打算覆蓋來自一個子類(或接口)的方法,所述編譯器。如果您不知道(例如因爲您更改了簽名而不再覆蓋),則會生成警告。你有沒有改變方法的簽名或從其他地方複製/移動它?

2

OnNewIntentActivity的功能,並且只能在extendsActivity的類別中被覆蓋。你正在擴展它在ProductListAdapter延伸Adapter

普萊斯代碼移動到上層階級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(); 
     } 
    } 
} 
0

您的項目配置爲使用JRE源代碼級別1.4(或更低)。要在Eclipse中解決這個問題

  1. 轉到Java項目的Properties在Eclipse
  2. 轉到Java Compiler菜單
  3. 檢查Compiler Compliance Level設置爲1.5或更高

剛想要讓您知道@Override註釋不需要覆蓋方法。

+0

將其設置爲1.6。謝謝你的提示 :) – user3070626

相關問題