Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

runOnUiThread is not running in AsyncTask

I'm coding a program which fetches the data from MySql from server (using JSON) and it updates the UI accordingly,

I'm fetching two types of data using AsyncTask from Server

1) Bubble Answers
2) Comments

The parseBubbleAnswers method successfully runs and Updates UI, but parseComments class which is AsyncTask, and which call parseComments method in doInBackground, is not running runOnUiThread(new Runnable() { run() });

Can anyone help me in solving this

Here is my code :

public class FetchServer extends Activity
{
    protected void onCreate(Bundle savedInstanceState) 
    {
        String photoId = "1"; // photo id for which the data is fetched
        checkBubbleData(photoId); // which call AsyncTask - 2 differnt calls
    }
    public void checkBubbleData(String photoId)
    {
        new parseBubbleAnswers().execute(photoId); // to fetch bubble answers
        new parseComments().execute(photoId); // to fetch comments
    }
    class parseBubbleAnswers extends AsyncTask<String, Integer,String> 
    {

        @Override
        protected String doInBackground(String... params) 
        {
            // TODO Auto-generated method stub
            Looper.prepare();
            parseBubbleAnswers(); // which has runOnUiThread(new Runnable() which updates (successfully !) the UI
            return null;
        }
    }
    class parseComments extends AsyncTask<String, Integer,String> 
    {

        @Override
        protected String doInBackground(String... params) 
        {
            // TODO Auto-generated method stub
            Looper.prepare();

            String parseComReturn = parseComments();
            if(parseComReturn=="end")
            {
                commentBuilder(); // which will update UI after fetch data by parseComments() method
            }
        }
    }
    public void commentBuilder()
    {
        runOnUiThread(new Runnable() // while debugging, it comes here, on Step Over it stick for 2 times and then move at the end of method without error 
        {       
            public void run() 
            {
                // update UI code
            }
        });
    }
}
like image 280
SML Avatar asked Dec 26 '12 08:12

SML


2 Answers

Try this way :

First create one Handler :

Handler mHandler = new Handler();

Change this,

public void commentBuilder()
    {
        runOnUiThread(new Runnable() // while debugging, it comes here, on Step Over it stick for 2 times and then move at the end of method without error 
        {       
            public void run() 
            {
                // update UI code
            }
        });
    }

With,

public void commentBuilder()
    {
        new Thread(new Runnable() {
            @Override
            public void run() {
                // TODO Auto-generated method stub
                while (isRunning) {
                    try {
                       // Thread.sleep(10000);
                        mHandler.post(new Runnable() {

                            @Override
                            public void run() {
                                // TODO Auto-generated method stub
                                // Write your code here to update the UI.                               
                            }
                        });
                    } catch (Exception e) {
                        // TODO: handle exception
                    }
                }
            }
        }).start();
    }

Stop thread by this once you are done with UI,

isRunning = false;

EDIT :

Try to Use Async Task in this way :

class parseComments extends AsyncTask<String, Integer,String> 
    {
        protected String doInBackground(String... params) {
            String parseComReturn = parseComments();
            return parseComReturn;
        }

        protected void onPostExecute(String result) {
            if(result.equals("end"))
            {
                commentBuilder();
            }
        }
    }

Thanks.

like image 70
Pratik Sharma Avatar answered Nov 15 '22 14:11

Pratik Sharma


runOnUiThread is a method of Activity, AsyncTask has no reference to Activity.

however, AsyncTask already runs on the UI thread and was designed to do exactly that.

just deal with the UI changes in onPostExecute.

like image 39
thepoosh Avatar answered Nov 15 '22 12:11

thepoosh