如何从AsyncTask更新TextView?

时间:2013-05-08 11:55:25

标签: android

这是我的代码:(一些随机文字来完成问题osdifhgsoid hgodfhgo hsdhoigifdshgnvfa oidvojd nobndisfn vbjobsf)。

private class DownloadFilesTask extends AsyncTask<String, Integer, Long> {
    protected Long doInBackground(String... urls) {
        try{
            Listen();
        }
        catch (Exception x)
        {
            textIn.setText("shit! " + x.toString());
        }
        long i = 10;
        return i;
    }
}

(一些随机文字再次完成问题(愚蠢系统)dpfgojd ipgsdigjsidoignsdog

public void Listen(){
    int count = 0;
    TextView msg = MyActivity.msg;
    ServerSocket server;
    Socket client;
    try {
        server = new ServerSocket(9797);
        Log.d("My log", "server started");
        Log.d("My log", "waiting for connnections");
        while (started) {
            try{
                msg.setText("waiting for connection"); <=== here crashing
                client = server.accept();
                count++;
                Log.d("My Log", "Connected");
                Log.d("My Log", "aha" + count);
                int i = 0;
                String data = null;
                byte[] bytes = new byte[1024];
                InputStream is = client.getInputStream();
                OutputStream os = client.getOutputStream();
                while (is.available() == 0) {
                    try{
                        Thread.sleep(50);
                    }catch (Exception cc){}
                }
                is.read(bytes, 0, is.available());
                os.write("hala".getBytes());
                client.close();
            }catch (Exception cc)
            {
                cc.toString();
            }
        }

    } catch (Exception el) {
        el.printStackTrace();
    }
}

(一些随机文字来完成问题)。请帮忙

6 个答案:

答案 0 :(得分:3)

通过onPostExecute方法更改它!

答案 1 :(得分:1)

AsyncTask的目的是在单独的线程中执行长时间运行的任务,然后通过onPostExecute()将结果传回UI线程。

另外,我不确定为什么你使用Long作为返回值,因为你似乎没有使用它。一个更好的解决方案是将Void作为返回值并保存异常并将其用作指示符,如果出现任何问题:

private class DownloadFilesTask extends AsyncTask<String, Integer, Void> {

    private Exception exception = null;

    @Override
    protected Void doInBackground(String... urls) {
        try{
            Listen();
        }
        catch (Exception x) {
            exception = x;
        }
    }

    @Override
    public void onPostExecute(Void result) {
        if(exception != null) {
            textIn.setText("shit! " + exception.toString());
        }
        else {
            // long running task was completed successfully
        }
    }
}

答案 2 :(得分:0)

是的,因为你试图在doInBackground()方法中设置TextView,这是不允许的。

如果你想在doInBackground()方法中设置TextView,在runOnUiThread方法中进行UI更新操作,那么就有了解决方案。

否则,建议在onPostExecute()方法中执行所有与UI显示/更新相关的操作,而不是AsyncTask类的doInBackground()方法。

答案 3 :(得分:0)

好主意是在doInBackground()中返回一个字符串,比如exceptionCatched。您可以在Exception块中将其设置为catch()标题,然后在onPostExecuted()中将其设置为if(!TextUtils.isEmpty(exceptionCatched)) textIn.setText(exceptionCatched);就是这样!

答案 4 :(得分:0)

private class DownloadFilesTask  extends AsyncTask<Void,    Void,Long>{

    @Override
    protected Long doInBackground(Void... params) {
        publishProgress(progress);
        //calculate progress and value from your  downloading logic 
    try {

        } catch (Exception e) {
            return (long) 0;
        }
        return null;
    }
    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
        //dis method run deafult on UI thread , so every time u publish ur onProgressUpdate will be called and update ur text here 
    }


     @Override
    protected void onPostExecute(Long result) {
        super.onPostExecute(result);
        if(result==0){
            //error occured
        }
    }

//如果发生异常,则返回结果作为long值以提示onPostExecute()

答案 5 :(得分:-1)

我在猜runOnUiThread。您无法从UI线程以外的任何其他线程更新UI。