runOnUiThread不AsyncTask的运行 [英] runOnUiThread is not running in AsyncTask

查看:117
本文介绍了runOnUiThread不AsyncTask的运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编码一个程序,它的从MySQL服务器从获取数据(使用JSON),它的更新UI 因此,

我用获取两种类型的数据的的AsyncTask 从服务器

  1)泡沫解答
2)点评

该parseBubbleAnswers方法成功运行,并更新用户界面,
但parseComments类,这是AsyncTask的,并调用parseComments方法doInBackground,没有运行 runOnUiThread(新的Runnable(){的run()});

谁能帮我解决这个

下面是我的code:

 公共类FetchServer扩展活动
{
    保护无效的onCreate(捆绑savedInstanceState)
    {
        串PHOTOID =1; //有照片的证件其数据被取
        checkBubbleData(PHOTOID); //其中要求的AsyncTask - 2调用不同的充
    }
    公共无效checkBubbleData(字符串PHOTOID)
    {
        新parseBubbleAnswers()执行(PHOTOID)。 //获取泡答案
        新parseComments()执行(PHOTOID)。 //提取评论
    }
    类parseBubbleAnswers扩展的AsyncTask<字符串,整数,字符串>
    {        @覆盖
        保护字符串doInBackground(字符串... PARAMS)
        {
            // TODO自动生成方法存根
            尺蠖prepare()。
            parseBubbleAnswers(); //其中有runOnUiThread(新的Runnable(),它的更新(成功!)的UI
            返回null;
        }
    }
    类parseComments扩展的AsyncTask<字符串,整数,字符串>
    {        @覆盖
        保护字符串doInBackground(字符串... PARAMS)
        {
            // TODO自动生成方法存根
            尺蠖prepare()。            串parseComReturn = parseComments();
            如果(parseComReturn ==结束)
            {
                commentBuilder(); //这将更新UI获取由parseComments数据之后()方法
            }
        }
    }
    公共无效commentBuilder()
    {
        runOnUiThread(新的Runnable()//调试时,说到这里,上步过它坚持了2次,然后在方法没有错误的端移动
        {
            公共无效的run()
            {
                //更新UI code
            }
        });
    }
}


解决方案

试试这个方法:

首先创建一个处理程序

 处理程序mHandler =新的处理程序();

更改此,

 公共无效commentBuilder()
    {
        runOnUiThread(新的Runnable()//调试时,说到这里,上步过它坚持了2次,然后在方法没有错误的端移动
        {
            公共无效的run()
            {
                //更新UI code
            }
        });
    }

随着

 公共无效commentBuilder()
    {
        新主题(新的Runnable接口(){
            @覆盖
            公共无效的run(){
                // TODO自动生成方法存根
                而(isRunning){
                    尝试{
                       //了Thread.sleep(10000);
                        mHandler.post(新的Runnable(){                            @覆盖
                            公共无效的run(){
                                // TODO自动生成方法存根
                                //在这里写下您的code更新UI。
                            }
                        });
                    }赶上(例外五){
                        // TODO:处理异常
                    }
                }
            }
        })。开始();
    }

该停止线程,一旦你与UI做的,

  isRunning = FALSE;

编辑:

尝试使用异步任务是这样的:

 类parseComments扩展的AsyncTask<字符串,整数,字符串>
    {
        保护字符串doInBackground(字符串... PARAMS){
            串parseComReturn = parseComments();
            返回parseComReturn;
        }        保护无效onPostExecute(字符串结果){
            如果(result.equals(结束))
            {
                commentBuilder();
            }
        }
    }

感谢。

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

解决方案

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.

这篇关于runOnUiThread不AsyncTask的运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆