Unable To Show Friends' Name On Chat
I have problem to show the friend name on my chat. the apps force close every time run to show the friend's name. if i comment the part for friend's name, the messages can appear f
Solution 1:
make the person name become dynamic
publicvoidshowPerson(String friendname, boolean leftSide) {
finalTextViewtextView=newTextView(ChatRoom.this);
textView.setTextColor(Color.BLACK);
textView.setTextSize(12);
textView.setText(friendname);
intbgName= R.id.meLabel;
LinearLayout.LayoutParamsparams=newLinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
if (!leftSide) {
bgName = R.id.friendLabel;
params.gravity = Gravity.RIGHT;
}
textView.setLayoutParams(params);
textView.setId(bgName);
runOnUiThread(newRunnable() {
@Overridepublicvoidrun() {
messagesContainer.addView(textView);
// Scroll to bottomif (scrollContainer.getChildAt(0) != null) {
scrollContainer.scrollTo(scrollContainer.getScrollX(), scrollContainer.getChildAt(0).getHeight());
}
scrollContainer.fullScroll(View.FOCUS_DOWN);
}
});
}
so the json part become like this
if(!content.equals("null")){
try{
JSONArray jArr = newJSONArray(content);
// String messages="";for(int i=0; i < jArr.length() ; i++){
JSONObject jObj = jArr.getJSONObject(i);
String name = jObj.getString("firstname");
String message = jObj.getString("message");
showPerson(name, false);
showMessage(message, false);
}
}catch(JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Solution 2:
All your setText, Toast, etc... actions should be done in the UI thread.
For instance, stuff like: friendLabel.setText(name);
In order to fix that, I would suggest you to do that inside the onPostExecute:
@OverrideprotectedvoidonPostExecute(JSONArray result) {
updateUI();// Do whatever you want here
}
From: http://developer.android.com/reference/android/os/AsyncTask.html
onPostExecute(Result), invoked on the UI thread after the background computation finishes. The result of the background computation is passed to this step as a parameter.
Post a Comment for "Unable To Show Friends' Name On Chat"