2013-04-25 51 views
1

我創建了一個測試android應用程序,它監聽RabbitMQ服務器上的主題並在textview中顯示消息。TextView在更新和滾動上繪製文本亂碼

所有的工作都很好,除了在添加到'輸出'textview的每條新消息上,新文本都被繪製在舊文本上。滾動時會發生同樣的情況,如第二張圖片所示。當應用程序轉到背景並再次激活時,文本再次顯示正常(請參見圖片1)。

normal output

garbled output

我的佈局非常簡單:我究竟做錯了什麼?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="60dp" > 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Connection status" /> 

<TextView 
    android:id="@+id/status" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:saveEnabled="false" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Received messages:" /> 

<ScrollView 
    android:id="@+id/scrollView1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 
    <TextView 
     android:id="@+id/output" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:saveEnabled="false" 
     android:textAppearance="?android:attr/textAppearanceSmall" /> 
</ScrollView> 

這裏是我處理在一個新的消息來了:

final Handler myViewUpdateHandler = new Handler() { 
    // @Override 
    public void handleMessage(Message msg) { 
     switch (msg.what) { 
     case NEWSTATUS: 
      status.setText((String) msg.obj); 
      status.invalidate(); 
      //also add to output 
     case NEWMESSAGE: 
      String oldText= (String) output.getText(); 
      output.setText(""); 

      String newText = DateUtil.getClockTimeMillis()+" "+(String) msg.obj+"\n"+oldText; 

      output.setText(newText); 
      output.invalidate(); 
      break; 
     default: 
      throw new IllegalArgumentException("no case for:" + msg.arg1); 
     } 
     super.handleMessage(msg); 
    } 
}; 

回答

1

我有同樣的確切問題。在我而言,這是發生了什麼事,我怎麼算出來:

  • 全屏我第一次添加第二個活動屬性(通過 蝕嚮導)

  • 然後我決定不使用那花哨的動畫全屏,所以我
    編輯佈局XML文件,並將其更改爲正常的相對視圖。

  • 應用程序清單文件表明此活動仍在使用全屏主題 。

  • 我刪除了這些行,並且......問題已解決。

如果您爲佈局設置了全屏主題,請嘗試將其更改爲應用主要主題。

+0

我完全按照你的描述做了,首先生成一個全屏活動,而不是正常的改變,而且樣式仍然是全屏。不幸的是,當項目關閉時(我們不再需要測試代碼),我無法再測試代碼。我認爲你的分析和解決方案是正確的。 – bluevoid 2013-07-27 14:41:11

0

嘗試刪除無效()方法,從所有的情況下調用,如果不解決這個問題,在你的XML把這個TextView的(ID =輸出)ScrollView屬性

android:cacheColorHint="#00000000" 
+0

感謝您的建議。我嘗試了兩個單獨的和一起:唉它不能解決我的問題。我被這個問題困擾,在我的其他應用程序中,我從來沒有這樣的東西。這應該是簡單的權利? – bluevoid 2013-04-25 08:36:03

+0

是的,通常你不應該有這個問題。另一個可能導致這種情況的問題是,可能你的處理程序刷新太多,以至於你的設備無法達到Handler的頻率,請嘗試通過下面的代碼降低Handler的頻率。 Handler handler = new Handler(); 處理程序。postDelayed(新的Runnable() \t \t { \t \t \t \t \t \t公共無效的run() \t \t \t {// 把你的更新代碼在這裏 }},5000(-or另一個號 - );它意味着你的處理程序將在每5000毫秒刷新一次,如果它看起來太快/慢,你可以修改它。 – 2013-04-25 08:47:28

+0

這對我來說不起作用,因爲我將textview的內容用作緩存(也許這是我的問題)? - > String newText = DateUtil.getClockTimeMillis()+「」+(String)msg.obj +「\ n」+ oldText; – bluevoid 2013-04-25 08:50:50