Java 为什么要获取TextView.append运行时异常?

Java 为什么要获取TextView.append运行时异常?,java,android,android-asynctask,textview,Java,Android,Android Asynctask,Textview,我有一个内部私有AsyncTask,它将访问外部全局TextView,并使用它在AsyncTask类中附加一个读取的字符串行。我可以很好地读入,但当我尝试将该行附加到TextView时,会出现致命的运行时异常 以下是异步任务代码: private class StreamTask extends AsyncTask<String, Void, Integer> { private static final int BUFFER_LENGTH = 1024 * 8; // Ad

我有一个内部私有AsyncTask,它将访问外部全局TextView,并使用它在AsyncTask类中附加一个读取的字符串行。我可以很好地读入,但当我尝试将该行附加到TextView时,会出现致命的运行时异常

以下是异步任务代码:

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;
    }
私有类StreamTask扩展了AsyncTask{
私有静态final int BUFFER_LENGTH=1024*8;//根据口味进行调整
字符串TAG2=“StreamTask”;
//参数#0=文件名
//参数#1=字符集名称
@凌驾
受保护的整数doInBackground(字符串…参数){
Log.d(TAG2,“在doInBackground中”);
如果(参数长度!=2){
Log.d(TAG2,“AsyncTask有2个参数”);
抛出新的IllegalArgumentException();
}
int chars=0;
CharsetDecoder cd=Charset.forName(参数[1]).newDecoder();
试一试{
Log.d(TAG2,“在第一个试块中”);
FileInputStream inputStream=null;
扫描仪sc=空;
试一试{
Log.d(TAG2,“在第二个试块中”);
inputStream=新文件inputStream(参数[0]);
Log.d(TAG2,“输入流集”+参数[0]);
sc=新扫描仪(inputStream,参数[1]);
Log.d(TAG2,“扫描仪集:“+params[1]);
while(sc.hasNextLine()){
Log.d(TAG2,“中间块”);
字符串行=sc.nextLine();
Log.d(TAG2,“该行为:”+行);
//系统输出打印项次(行);
textView.append(line);//这就是问题所在~~~
}
//请注意,扫描仪会抑制异常
如果(sc.ioException()!=null){
Log.d(TAG2,“异常”);
抛出sc.ioException();
}
} 
catch(filenotfound异常){}
最后{
Log.d(TAG2,“最后……”);
如果(inputStream!=null){
inputStream.close();
}
如果(sc!=null){
sc.close();
}
}
}
捕获(IOE){}
返回字符;
}
提前谢谢


Jason

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

您可以更新UI线程上运行的UI fom
onPostExecute()
。如

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


如果您需要仅从doInBackground更新UI,您可以使用下面的
runOnUiThread

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


是一篇好文章。

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

您可以更新UI线程上运行的UI fom
onPostExecute()
。如

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


如果您需要仅从doInBackground更新UI,您可以使用下面的
runOnUiThread

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


是一篇好文章。

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

您可以更新UI线程上运行的UI fom
onPostExecute()
。如

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


如果您需要仅从doInBackground更新UI,您可以使用下面的
runOnUiThread

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


是一篇好文章。

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

您可以更新UI线程上运行的UI fom
onPostExecute()
。如

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


如果您需要仅从doInBackground更新UI,您可以使用下面的
runOnUiThread

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

这是一篇好文章