我有一个活动,使用RoboGuice打印出日志。其中有一个TextView,它显示从文件中读取的大型文本字符串。
问题是我想使用textView.append()添加新行,但没有任何反应。 textView.setText工作正常但我不能使用它,因为文本重置很慢并且消耗了CPU。
我尝试了很多东西。在UI线程上运行它,使视图无效,使用view.post(Runnable)并在UI线程中的Handler上发布它,但没有任何帮助。
以下是代码示例:
public class DiagnosticsActivity extends RoboActionBarActivity implements LogListener {
@Inject
private LogMessageHandler logger;
@InjectView(R.id.logView)
TextView logView;
@InjectView(R.id.logScrollView)
ScrollView scrollView;
public boolean logFileReadFinished = false;
private StringBuilder textUntilReadFinished = new StringBuilder();
private File sdcard;
private class AppendRunnable implements Runnable {
private String msg;
private AppendRunnable(String msg) {
this.msg = msg;
}
public void run() {
logView.append(msg);
logView.invalidate();
}
};
@Override
protected void onResume() {
logger.registerLogListener(this);
readLogFile();
scrollView.scrollTo(0, Integer.MAX_VALUE);
synchronized (this) {
final String stringUntilReadFinished = textUntilReadFinished.toString();
textUntilReadFinished = new StringBuilder();
DiagnosticsActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
Handler mHandler = new Handler();
mHandler.post(new AppendRunnable(stringUntilReadFinished + "\n"));
}
});
logFileReadFinished = true;
}
super.onResume();
}
@Override
protected void onPause() {
logger.removeLogListener();
logFileReadFinished = false;
super.onPause();
}
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.activity_diagnostics);
ActionBar actionBar = getSupportActionBar();
actionBar.show();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setTitle(R.string.title_diagnostics);
sdcard = Environment.getExternalStorageDirectory();
}
private void readLogFile() {
FileInputStream fileInputStream = null;
BufferedReader reader = null;
try {
final StringBuilder text = new StringBuilder();
File file = new File(sdcard, LoggerImpl.logName + ".log");
if (file.exists()) {
try {
fileInputStream = new FileInputStream(file);
reader = new BufferedReader(new InputStreamReader(fileInputStream));
String line = null;
while ((line = reader.readLine()) != null) {
text.append(line + "\n");
}
DiagnosticsActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
logView.setText(text);
}
});
} catch (FileNotFoundException e) {
logger.error("Log file not found!", e);
} catch (IOException e) {
logger.error("Cannot read log file!", e);
}
}
} finally {
try {
reader.close();
fileInputStream.close();
} catch (IOException e) {
logger.error("Cannot close log file", e);
}
}
}
@Override
public void onLogEvent(final String msg, Throwable throwable) {
synchronized (this) {
if (logFileReadFinished) {
DiagnosticsActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
Handler mHandler = new Handler();
mHandler.post(new AppendRunnable(msg + "\n"));
}
});
} else {
textUntilReadFinished.append(msg + "\n");
}
}
}
答案 0 :(得分:0)
在致电msg
时,您是否检查过以确保append()
不为空?
作为替代方法,您可以使用以下内容更新TextView
而不删除旧信息:
logView.setText(logView.getText() + msg);
以上代码段将获取当前显示的文字,附加新邮件,然后显示整个String
。