runOnUiThread未在AsyncTask中运行

时间:2012-12-26 08:44:26

标签: android multithreading

我正在编写一个程序,从服务器(使用JSON)从MySql获取数据,并相应地更新UI

我从服务器

使用 AsyncTask 获取两种类型的数据
1) Bubble Answers
2) Comments

parseBubbleAnswers方法成功运行并更新UI, 但parseComments类是AsyncTask,在doInBackground中调用parseComments方法,没有运行runOnUiThread(new Runnable() { run() });

任何人都可以帮我解决这个问题

这是我的代码:

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

3 个答案:

答案 0 :(得分:8)

尝试这种方式:

首先创建一个Handler

Handler mHandler = new Handler();

更改此内容,

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

。通过,

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

完成UI后停止线程,

isRunning = false;

编辑:

尝试以这种方式使用异步任务:

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

感谢。

答案 1 :(得分:6)

runOnUiThreadActivity的方法,AsyncTask没有引用活动。

然而,AsyncTask已经在UI线程上运行,并且正是为此而设计的。

只需处理onPostExecute中的用户界面更改。

答案 2 :(得分:0)

我遇到了类似的问题。

只需将Activity类的引用传递给parseComments类。

class parseComments extends AsyncTask<String, Integer,String>{

 Activity activity;
 public parseComments(Activity activity){
  this.activity = activity;
 }
}

之后,您可以将runOnUiThread用作

activity.runOnUiThread(new Runnable(){
  @Override
  public void run(){

  }
});

仅适用于Activity类。不是上下文类。