skip to Main Content

I want to execute a search command this way but indicate error, any other way to modify this

```public class UserActivity extends AppCompatActivity {
  private ImageView im;
  protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.userpage);
  im = (ImageView) findViewById(R.id.search);
  im.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
           SearchTask();
        }
   });
  static class SearchTask extends AsyncTask<Void, Void, Void>{
        //TO DO
    }
 }```

Unfortunately the static class always indicates an error that is:Method call expected

2

Answers


  1. SearchTask is a class and you need to instantiate it before use.

    Replace

    SearchTask();
    

    with

    final SearchTask cSearchTask = new SearchTask();
    cSearchTask.execute(...);
    

    PS: pay attention that AsyncTask are deprecated in newer Android 11+ (The AsyncTask API is deprecated in Android 11. What are the alternatives?)

    Login or Signup to reply.
  2. Your AsyncTask Implementation is wrong

    Example Snippet from SO answer

    private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
         protected Long doInBackground(URL... urls) {
             int count = urls.length;
             long totalSize = 0;
             for (int i = 0; i < count; i++) {
                 totalSize += Downloader.downloadFile(urls[i]);
                 publishProgress((int) ((i / (float) count) * 100));
                 // Escape early if cancel() is called
                 if (isCancelled()) break;
             }
             return totalSize;
         }
    
         protected void onProgressUpdate(Integer... progress) {
             setProgressPercent(progress[0]);
         }
    
         protected void onPostExecute(Long result) {
             showDialog("Downloaded " + result + " bytes");
         }
     }
    

    Then use new DownloadFilesTask().execute(url1, url2, url3);

    Change your code like so

    im.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
               new SearchTask().execute(url1,url2,url3);
            }
       });
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search