Search code examples
androidsuppress-warnings

Varargs methods warning


I have this doInBackground in my async task and I'm getting this Vargas warning :

Varargs methods should only override or be overridden by other varargs methods unlike RingBankAsyncTask.doInBackground(String[]) and AsyncTask.doInBackground(String...)

   protected String doInBackground(String[] urls){
   String result = "";
   for (int i = 0; i <= 0; i++){
     result = invokePost(urls[i], this.postData);
   }
   return result;
 }

I know why I'm getting this warning but Is there a workaround to get rid of this !?


Solution

  • change

    protected String doInBackground(String[] urls)
    

    to

    protected String doInBackground(String... urls)
    

    varargs means a varying length of arguments, so although you use it like an array on the receiving end, you supply params like the following (if you were manually calling the method):

    doInbackground(val1, val2, val3);
    

    vs

    doInbackground(new String[] { val1, val2, val3 });
    

    As a rule, you can always look at the supertype when overriding a method to make sure you match it's signature, and for many other useful things with other problems too. Just make sure you have sources installed.