0

Here is my code in the activity in my project:

    CustomClass custom = new CustomClass(); 
    //Passing an array list
    result = custom.getResults(list);

Here CustomClass is a class in the library project and the method getResults(list) starts an AsyncTask.

getResults() method :

    public List<String> getResults(List<String> string){        
        List<String> result= new ArrayList<String>();
        BackgroundTask task =  new BackgroundTask();
        task.execute(string);
        result= task.get();
        return result;
    }

My Async task :

    public class BackgroundTask extends AsyncTask<List<String>, Integer, List<String> >{
    List<String> results= new ArrayList<String>();
    @Override
    protected List<String> doInBackground(List<String>... params) {
        for (int i=0; i<params[0].size();i++) {
            // someOperation
        }
        return results; 
    }
    @Override
    protected void onPostExecute(List<String> result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
    }
    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
    }
    @Override
    protected void onProgressUpdate(Integer... values) {
        // TODO Auto-generated method stub
        super.onProgressUpdate(values);
    }


}

Problem : The AsyncTask is not started.When I debug and try to enter into the AsyncTask class I get The source attachment does not contain the source for the file classloader.class. Can someone help me here ? thank you.

quad
  • 872
  • 3
  • 17
  • 37
  • AsyncTask is not designed for such use-case. Go through this link for more information: http://developer.android.com/reference/android/os/AsyncTask.html – Nitin Sethi Sep 13 '13 at 17:22
  • 1
    @NitinSethi This looks like perfectly valid use to me. – kabuko Sep 13 '13 at 17:32
  • Do you get any exception//crash? – codeMagic Sep 13 '13 at 17:35
  • No crashes or exception.. – quad Sep 13 '13 at 17:38
  • AsyncTask must be created and execute called on the UI thread. Is the GetResults method called on the UI thread? – Dale Wilson Sep 13 '13 at 17:41
  • Yes..It is called from my main activity.. – quad Sep 13 '13 at 17:45
  • Using `.get()` could be a problem...it will block the `UI`. You should use an [interface instead](http://stackoverflow.com/questions/18517400/inner-class-can-access-but-not-update-values-asynctask). But this wouldn't keep the task from executing. You have put a breakpoint at the first line in `doInBackground()`? – codeMagic Sep 13 '13 at 17:46
  • If you aren't positive that its not getting called and you just aren't getting the result then `get()` may the issue – codeMagic Sep 13 '13 at 17:53

1 Answers1

1

The get() method blocks the UI thread which means onPreExecute can't run (it has to run in the UI thread) Thus the background thread will never be started.

Get rid of the call to get() and move the "return the value" logic to onPostExecute.

And NEVER block when you are in the UI thread.

Dale Wilson
  • 9,166
  • 3
  • 34
  • 52
  • Background Information. The UI thread runs an event loop (see Android Looper and Handler for details) This is equivalent to the event loop in Windows/MacOS/etc. – Dale Wilson Sep 13 '13 at 17:52
  • The event loop only processes one event at a time -- right now it is processing whatever user action called GetResults(). Calling execute() on the async task posts a new event to be processed -- the one that will call onPreExecute. As soon as this event is complete, then then the next one can be dispatched. – Dale Wilson Sep 13 '13 at 17:54
  • onPreExecute starts a thread (or picks one from a thread pool) This thread also runs a looper. onPreExecute posts a message to the background thread to actually call doInBackground. – Dale Wilson Sep 13 '13 at 17:57
  • By not returning from GetResults you are blocking this entire mechanism. – Dale Wilson Sep 13 '13 at 17:58