【问题标题】:Why do I get TextView.append runtime exception?为什么会出现 TextView.append 运行时异常?
【发布时间】:2014-08-08 09:30:09
【问题描述】:

我有一个内部私有 AsyncTask,它将访问外部全局 TextView 并使用它来附加在该 AsyncTask 类中读取的字符串行。我可以正常阅读,但是当我尝试将该行附加到 TextView 时,我得到了致命的运行时异常。

这里是 AsyncTask 代码:

private class StreamTask extends AsyncTask<String, Void, Integer> {
    private static final int BUFFER_LENGTH = 1024 * 8; // Adjust to taste
    String TAG2 = "StreamTask";
    // Param #0 = file name
    // Param #1 = charset name
    @Override
    protected Integer doInBackground(String... params) {
        Log.d(TAG2, "in doInBackground");

        if (params.length != 2) {
            Log.d(TAG2, "AsyncTask has 2 params");
            throw new IllegalArgumentException();
        }

        int chars = 0;
        CharsetDecoder cd = Charset.forName(params[1]).newDecoder();
        try{
            Log.d(TAG2, "in first try block");
            FileInputStream inputStream = null;
            Scanner sc = null;
            try {
                Log.d(TAG2, "in second try block");
                inputStream = new FileInputStream(params[0]);
                Log.d(TAG2, "inputstream set "+params[0]);
                sc = new Scanner(inputStream, params[1]);
                Log.d(TAG2, "scanner set: "+params[1]);
                while (sc.hasNextLine()) {
                    Log.d(TAG2, "in while block");
                    String line = sc.nextLine();
                    Log.d(TAG2, "this line is: "+line);
                    // System.out.println(line);
                    textView.append(line);//This is where the problem is~~~
                }
                // note that Scanner suppresses exceptions
                if (sc.ioException() != null) {
                    Log.d(TAG2, "throws ioException");
                    throw sc.ioException();
                }
            } 
            catch(FileNotFoundException e){}

            finally {
                Log.d(TAG2, "in finally...");
                if (inputStream != null) {
                    inputStream.close();
                }
                if (sc != null) {
                    sc.close();
                }
            }
        }
        catch(IOException e){}  
        return chars;
    }

提前致谢!

杰森

【问题讨论】:

    标签: java android android-asynctask textview


    【解决方案1】:

    您无法从 AsynTask 的 doInBackground() 更新 UI。因为它运行在其他线程上(不能直接更新 UI)。

    您可以更新在 UI 线程上运行的 onPostExecute() 的 UI。喜欢

    @Override
    protected void onPostExecute(String b){
        textView.append(b);
    }
    

    了解AsynTask


    如果您只需要从 doInBackground 更新 UI,您可以使用runOnUiThread,如下所示

    runOnUiThread(new Runnable() {
         public void run() {
                textView.append(line);
         }
    });
    

    Communicating with the UI Thread 是一篇好文章。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-26
      • 2016-06-15
      • 1970-01-01
      • 2023-03-10
      • 1970-01-01
      • 1970-01-01
      • 2016-11-02
      • 2022-01-03
      相关资源
      最近更新 更多