Android 打印日志和不打印日志之间发生了奇怪的事情
我在运行应用程序时遇到了一个奇怪的问题。Android 打印日志和不打印日志之间发生了奇怪的事情,android,eclipse,Android,Eclipse,我在运行应用程序时遇到了一个奇怪的问题。 我有一个代码可以把段落分成几行。当运行它而不在每行代码下面打印日志时,我的代码运行完全错误。当尚未达到行的宽度时,它会断开行,创建类似以下的段落: Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, qu
我有一个代码可以把段落分成几行。当运行它而不在每行代码下面打印日志时,我的代码运行完全错误。当尚未达到行的宽度时,它会断开行,创建类似以下的段落:
Lorem ipsum dolor
sit amet, consectetur adipisicing elit,
sed do eiusmod tempor incididunt ut labore
et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud.
但当我插入日志以打印每行代码的结果时,我的代码运行得几乎完美,如下所示:
Lorem ipsum dolor sit amet, consectetur
adipisicing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud.
我的日志只是为了打印每个变量的值,它们不计算任何内容
我的问题是:你们有没有遇到过这样的情况,在运行应用程序时打印日志和不打印日志的结果不同?为什么会这样?如何解决
多谢各位
更新
下面是我的代码,它将段落分成了几行。使用boolean log=false
,它将返回错误的结果
public static ArrayList<Row> getRows(int displayWidth, String text, TextStyle style) {
// Split to words
ArrayList<String> punctuations = new ArrayList<String>();
ArrayList<Row> rows = new ArrayList<Row>();
boolean log = true;
text = text.replace("\\r\\n", "");
ArrayList<String> words = new ArrayList<String>();
Pattern p = Pattern.compile(REGEX);
Matcher m = p.matcher(text);
int lastCutIndex = 0;
while (m.find()) {
words.add(text.substring(lastCutIndex, m.start()));
if (m.end() <= text.length()) {
punctuations.add(text.substring(m.start(), m.end()));
}
lastCutIndex = m.end();
}
if (lastCutIndex < text.length()) words.add(text.substring(lastCutIndex, text.length()));
// Fill words into row
int full = getSize("a b", style)[0];
int less = getSize("ab", style)[0];
int spaceCharWidth = full - less;
int currentRow = 0;
rows.add(new Row(style.getTypeface(), style.getTextSize()));
for (int i = 0; i < words.size(); i++) {
int[] rowSize = getSize(rows.get(currentRow).getText(), style);
int rowWidth = rowSize[0];
int rowHeight = rowSize[1];
rows.get(currentRow).lineHeight =
FastMath.round(rowHeight * style.line_mult + style.line_add);
String word = words.get(i);
String punc = "";
if (i < punctuations.size()) punc = punctuations.get(i);
if (log) {
Logger.e(TAG, "word:" + word + ",punc:" + punc + "|");
}
int puncWidth = 0;
if (punc.equals(" ")) puncWidth = spaceCharWidth;
else puncWidth = getSize(punc, style)[0];
int nextWordWidth = getSize(word, style)[0];
if (log) {
Logger.e(TAG, "nexWordWidth:" + nextWordWidth);
}
int predictWidth = FastMath.round(rowWidth + nextWordWidth
+ (rows.get(currentRow).elementCount - 1) * WIDTH_ADD_RATIO);
if (log) {
Logger.e(TAG, "predictWidth:" + predictWidth);
}
if (punc.equals(" ")) {
if (log) {
Logger.e(TAG, "predict is < displayWidth?" + ((predictWidth < displayWidth) ? true : false));
}
if (predictWidth < displayWidth) {
rows.get(currentRow).append(word);
if (predictWidth + puncWidth < displayWidth) {
rows.get(currentRow).append(punc);
}
if (log) {
Logger.e(TAG, "rows[" + currentRow + "]:" + rows.get(currentRow).getText());
}
} else {
currentRow++;
if (log) {
Logger.e(TAG, "new row");
}
Row row = new Row(style.getTypeface(), style.getTextSize());
row.append(word);
if (word.equals("")) {
if (!punc.equals(" ")) {
row.append(punc);
}
} else {
row.append(punc);
}
rows.add(row);
if (log) {
Logger.e(TAG, "rows[" + currentRow + "]:" + rows.get(currentRow).getText());
}
}
} else {
if (log) {
Logger.e(TAG, "predict + puncWidth is < displayWidth?" + ((predictWidth < displayWidth) ? true : false));
}
if (predictWidth + puncWidth < displayWidth) {
rows.get(currentRow).append(word);
rows.get(currentRow).append(punc);
if (log) {
Logger.e(TAG, "rows[" + currentRow + "]:" + rows.get(currentRow).getText());
}
} else {
currentRow++;
if (log) {
Logger.e(TAG, "new row");
}
Row row = new Row(style.getTypeface(), style.getTextSize());
row.append(word);
if (word.equals("")) {
if (!punc.equals(" ")) {
row.append(punc);
}
} else {
row.append(punc);
}
rows.add(row);
if (log) {
Logger.e(TAG, "rows[" + currentRow + "]:" + rows.get(currentRow).getText());
}
}
}
}
return rows;
}
public static int[] getSize(String text, TextStyle style) {
if (text == null) return new int[2];
Paint paint = new Paint();
paint.setTextSize(style.getTextSize());
paint.setTypeface(style.getTypeface());
Rect rect = new Rect();
paint.getTextBounds(text.toCharArray(), 0, text.length(), rect);
int[] result = new int[2];
result[0] = rect.width();
result[1] = rect.height();
return result;
}
publicstaticarraylistgetrows(int-displayWidth、stringtext、TextStyle){
//支离破碎
ArrayList标点=新的ArrayList();
ArrayList行=新的ArrayList();
布尔对数=真;
text=text.replace(“\\r\\n”和“”);
ArrayList words=新的ArrayList();
Pattern p=Pattern.compile(REGEX);
匹配器m=p.Matcher(文本);
int lastCutIndex=0;
while(m.find()){
words.add(text.substring(lastCutIndex,m.start());
如果(m.end()怀疑你的代码…缩小到显示行为的代码的一小部分,并在此处发布…我同意@MitchWheat,我认为这是你的代码中的一个问题。@MitchWheat&Sean F:我更新了我的代码。我们真的不想检查你的所有代码!…我在发布问题之前就这样做了,所以我没有将它放在第一位,但这样做了有时我会遇到这样的问题,不仅仅是在这样拆分段落的时候,而是在计算这个或那个的时候,它发生了,我不知道为什么!所以我想我可能会问,也许有人会有答案。无论如何,谢谢你。