2012-09-27 150 views
1

我正在嘗試基於smack庫創建一個聊天應用程序。我認爲我的問題與這個庫無關,因爲logcat正確地發送和接收所有內容。我的問題是當我收到它不顯示的消息時。以下是我用來顯示文本的方法。在Android中顯示動態Textview

public void TextCreating(String message) { 

    Log.d("START", "Method Excution started."); 

    layout = (LinearLayout) findViewById(R.id.layoutLinear); 
    LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, 
      LayoutParams.WRAP_CONTENT); 
    tv = new TextView(this); 
    tv.setLayoutParams(lparams); 
    tv.setText(message); 
    tv.setTextColor(Color.WHITE); 
    Log.d("STARTED", "Text color OK."); 
    layout.addView(tv); 
    Log.d("STARTED", "Text didsplayed."); 
} 

代碼的和平將作爲收到的消息執行。那些收到的消息通過一個名爲PacketListener的監聽器進行處理。這個接收部分工作正常。在這個監聽器中,當我調用上面的TextCreating方法時,它執行到tv.setTextColor(Color.WHITE);最後一行不執行。這是什麼原因?我怎麼解決這個問題 ?感謝提前:)

編輯:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Chat Room" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="10dp" 
    android:text="Chat History :" /> 

<LinearLayout 
    android:id="@+id/layoutLinear" 
    android:layout_width="fill_parent" 
    android:layout_height="250dp" 
    android:orientation="vertical" 
    android:background="#000000" 
    android:layout_marginLeft="2dp" 
    android:layout_marginRight="2dp" > 

</LinearLayout> 

<EditText 
    android:id="@+id/editTextMessage" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="5dp" 
    android:ems="10" > 

    <requestFocus /> 
</EditText> 

<Button 
    android:id="@+id/buttonSend" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Send" /> 

</LinearLayout> 
+1

你是什麼意思'行不執行'?如果它被編譯並上傳到設備,那麼如果它沒有發生異常,它應該被執行。你有沒有試圖在調試器中設置斷點? – sandrstar

+0

Log.d(「STARTED」,「文本didsplayed。」);不記錄。但Log.d(「STARTED」,「文本顏色確定。」);日誌。因爲我覺得這意味着layout.addView(tv);不執行... – Chamath

+0

layoutLinear只是一個正常的LinearLayout而不是一些擴展版本? 「文本顏色確定」後日志中的任何例外情況? – sandrstar

回答

0

首先謝謝你的每一個你的想法和幫助。我通過向代碼引入Handler來解決這個問題。我試圖在我的UI線程中做所有事情。所以我修改我的代碼如下。

private Handler mHandler = new Handler(); 
public void TextCreating(final String message) { 

    mHandler.post(new Runnable() { 
     @Override 
     public void run() {    
      layout = (LinearLayout) findViewById(R.id.layoutLinear); 
      LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, 
        LayoutParams.WRAP_CONTENT); 
      tv = new TextView(MUCchat.this); 
      tv.setLayoutParams(lparams); 
      tv.append(message); 
      tv.setTextColor(Color.WHITE); 
      layout.addView(tv); 
     } 
    });  
} 

此鏈接幫助我瞭解了很多。 Update textView from thread

+0

你是否說過你試圖不從UI線程更新它?通常(似乎並不總是,例如不是在舊的Android版本)有導致異常,但你已經提到沒有觀察到異常... – sandrstar

+0

是的,這是問題所在。我正在爲API10開發這個版本,因爲我之前告訴過你,這個問題上沒有例外。消息發送和接收在logcat上工作,儘管它們沒有在仿真器上顯示。非常感謝你sandrstar。 – Chamath

0

這裏是一個替代的解決方案來創建一個聊天框。 它的用途ListView。將新項目添加爲message

android:stackFromBottom 

可以設置爲true,使其完全像一個聊天框。默認情況下它也具有滾動功能。

希望它有幫助。

+0

此聊天應用程序可能包含單選按鈕,複選框等的消息,在這種情況下,可能很難將此方法用作此解決方案。在這裏我只嘗試過基本的文字聊天。無論如何感謝您的想法... – Chamath

0

Android SDK有一個示例聊天應用程序,它通過藍牙進行通信。 你可以看看代碼。

它採用了ListView顯示對話

希望這個答案是非常有用的:)

+0

感謝您的想法。但我想知道爲什麼會出現上述問題。 – Chamath

0

我已經運行在設備上此致代碼,並沒有發現問題與它。我已經做了TextCreating(String.valueOf(System.currentTimeMillis()) + " next call\n");的定時器調用,並能夠觀察黑色LinearLayout中的文本。
我看到的唯一問題是我看不到LinearLayout不會滾動。所以,如果你添加更多的文字,它將不可見。