unable to show friends' name on chat

后端 未结 2 916
清歌不尽
清歌不尽 2021-01-29 04:29

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 app

相关标签:
2条回答
  • 2021-01-29 05:11

    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:

        @Override
        protected void onPostExecute(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.

    0 讨论(0)
  • 2021-01-29 05:26

    make the person name become dynamic

    public void showPerson(String friendname, boolean leftSide) {
            final TextView textView = new TextView(ChatRoom.this);
            textView.setTextColor(Color.BLACK);
            textView.setTextSize(12);
            textView.setText(friendname);
    
            int bgName = R.id.meLabel;
    
            LinearLayout.LayoutParams params = new LinearLayout.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(new Runnable() {
                @Override
                public void run() {
                    messagesContainer.addView(textView);
    
                    // Scroll to bottom
                    if (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 = new JSONArray(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();
                            }
                        }
    
    0 讨论(0)
提交回复
热议问题