我正在嘗試基於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>
你是什麼意思'行不執行'?如果它被編譯並上傳到設備,那麼如果它沒有發生異常,它應該被執行。你有沒有試圖在調試器中設置斷點? – sandrstar
Log.d(「STARTED」,「文本didsplayed。」);不記錄。但Log.d(「STARTED」,「文本顏色確定。」);日誌。因爲我覺得這意味着layout.addView(tv);不執行... – Chamath
layoutLinear只是一個正常的LinearLayout而不是一些擴展版本? 「文本顏色確定」後日志中的任何例外情況? – sandrstar