0

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 fine. but when i add the label for name, it got error and force close. i really need help, anyone please help me. thank you

json part

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");

                                     friendLabel.setText(name);
                                     showMessage(message, false);
                              }
                        }catch(JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                    else{
                        Toast.makeText(ChatRoom.this, "Error", Toast.LENGTH_LONG).show();
                     }

whole code

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.chatpage);

        messagesContainer = (ViewGroup) findViewById(R.id.messagesContainer);
        scrollContainer = (ScrollView) findViewById(R.id.scrollContainer);

        Button sendMessageButton = (Button) findViewById(R.id.sendButton);

        Bundle bundle = this.getIntent().getExtras();
        /*final String paramnama = bundle.getString("nama");*/
        messageText = (EditText) findViewById(R.id.messageEdit);
        meLabel = (TextView) findViewById(R.id.meLabel);
        friendLabel = (TextView) findViewById(R.id.friendLabel);
        meLabel.setText("me");


        final String param1 = bundle.getString("keyCourseId");
        final String param2 = bundle.getString("keyUserId");
        final String param3 = bundle.getString("keyChatsId");
         String param4 = bundle.getString("keyMessagesId");


        sendMessageButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                //for send part already works fine
        });

        HttpURLConnection connection;
        URL url = null;
        try{
            linkurl = new Koneksi(this);
            SERVER_URL = linkurl.getUrl();
            SERVER_URL += "/mobile/ChatRoom.php?idu="+param2+"&idch="+param3;
            url = new URL(SERVER_URL);
            connection = (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            connection.setRequestMethod("POST");
            new ReceivedTask().execute();

            }
        catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

            }


    public class ReceivedTask extends AsyncTask<String, String, String> {

        @Override
        protected void onPreExecute() {

        }

        @Override
        protected String doInBackground(String... arg0) {
            // TODO Auto-generated method stub
            try{
                HttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(SERVER_URL);
                ArrayList<NameValuePair> param = new ArrayList<NameValuePair>();
                //add parameter
                    httpPost.setEntity(new UrlEncodedFormEntity(param));

                    HttpResponse httpRespose = httpClient.execute(httpPost);
                    HttpEntity httpEntity = httpRespose.getEntity();

                    //read content
                    InputStream in = httpEntity.getContent();
                    BufferedReader read = new BufferedReader(new InputStreamReader(in));

                    String content = "";
                    String line = "";


                    while((line = read.readLine())!=null){
                       content += line;
                    }
                   Log.d("ADBUG", "content: "+content);

                   //json
                    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 message = jObj.getString("message");


                                     showMessage(message, false);
                              }
                        }catch(JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                    else{
                        Toast.makeText(ChatRoom.this, "Error", Toast.LENGTH_LONG).show();
                     }
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();}
            catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }



            return null;

                }
        }



    public void showMessage(String message, boolean leftSide) {
        final TextView textView = new TextView(ChatRoom.this);
        textView.setTextColor(Color.BLACK);
        textView.setText(message);

        int bgRes = R.drawable.left_message_bg;

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

        if (!leftSide) {
            bgRes = R.drawable.right_message_bg;
            params.gravity = Gravity.RIGHT;
        }

        textView.setLayoutParams(params);

        textView.setBackgroundResource(bgRes);

        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);
            }
        });
    }

LogCat

06-22 17:16:06.688: D/ADBUG(698): content: [{"firstname":"teach","message":"test"},{"firstname":"teach","message":"test"},{"firstname":"teach","message":"test"},{"firstname":"teach","message":"testing chat"},
06-22 17:16:06.787: W/dalvikvm(698): threadid=11: thread exiting with uncaught exception (group=0x40015560)
06-22 17:16:06.797: E/AndroidRuntime(698): FATAL EXCEPTION: AsyncTask #2
06-22 17:16:06.797: E/AndroidRuntime(698): java.lang.RuntimeException: An error occured while executing doInBackground()
06-22 17:16:06.797: E/AndroidRuntime(698):  at android.os.AsyncTask$3.done(AsyncTask.java:200)
06-22 17:16:06.797: E/AndroidRuntime(698):  at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
06-22 17:16:06.797: E/AndroidRuntime(698):  at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
06-22 17:16:06.797: E/AndroidRuntime(698):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
06-22 17:16:06.797: E/AndroidRuntime(698):  at java.util.concurrent.FutureTask.run(FutureTask.java:138)
06-22 17:16:06.797: E/AndroidRuntime(698):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
06-22 17:16:06.797: E/AndroidRuntime(698):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
06-22 17:16:06.797: E/AndroidRuntime(698):  at java.lang.Thread.run(Thread.java:1019)
06-22 17:16:06.797: E/AndroidRuntime(698): Caused by: android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
06-22 17:16:06.797: E/AndroidRuntime(698):  at android.view.ViewRoot.checkThread(ViewRoot.java:2932)
06-22 17:16:06.797: E/AndroidRuntime(698):  at android.view.ViewRoot.requestLayout(ViewRoot.java:629)
06-22 17:16:06.797: E/AndroidRuntime(698):  at android.view.View.requestLayout(View.java:8267)
06-22 17:16:06.797: E/AndroidRuntime(698):  at android.view.View.requestLayout(View.java:8267)
06-22 17:16:06.797: E/AndroidRuntime(698):  at android.view.View.requestLayout(View.java:8267)
06-22 17:16:06.797: E/AndroidRuntime(698):  at android.view.View.requestLayout(View.java:8267)
06-22 17:16:06.797: E/AndroidRuntime(698):  at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:257)
06-22 17:16:06.797: E/AndroidRuntime(698):  at android.view.View.requestLayout(View.java:8267)
06-22 17:16:06.797: E/AndroidRuntime(698):  at android.widget.ScrollView.requestLayout(ScrollView.java:1291)
06-22 17:16:06.797: E/AndroidRuntime(698):  at android.view.View.requestLayout(View.java:8267)
06-22 17:16:06.797: E/AndroidRuntime(698):  at android.view.View.requestLayout(View.java:8267)
06-22 17:16:06.797: E/AndroidRuntime(698):  at android.widget.TextView.checkForRelayout(TextView.java:5521)
06-22 17:16:06.797: E/AndroidRuntime(698):  at android.widget.TextView.setText(TextView.java:2724)
06-22 17:16:06.797: E/AndroidRuntime(698):  at android.widget.TextView.setText(TextView.java:2592)
06-22 17:16:06.797: E/AndroidRuntime(698):  at android.widget.TextView.setText(TextView.java:2567)
06-22 17:16:06.797: E/AndroidRuntime(698):  at mobile.chat.ChatRoom$ReceivedTask.doInBackground(ChatRoom.java:199)
06-22 17:16:06.797: E/AndroidRuntime(698):  at mobile.chat.ChatRoom$ReceivedTask.doInBackground(ChatRoom.java:1)
06-22 17:16:06.797: E/AndroidRuntime(698):  at android.os.AsyncTask$2.call(AsyncTask.java:185)
06-22 17:16:06.797: E/AndroidRuntime(698):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
06-22 17:16:06.797: E/AndroidRuntime(698):  ... 4 more
blackneko
  • 91
  • 1
  • 12
  • Did you found a solution already? – Bigflow Jun 24 '13 at 07:48
  • @Bigflow, thank you very much for your respond. Yes I already found it, I must make for the person name become dynamic. :) – blackneko Jun 24 '13 at 09:45
  • If you got an answer to the question, you can also make your own answer and accept it. It can always help other people on how you fixed it :) – Bigflow Jun 24 '13 at 09:48
  • @Bigflow, can I ask more question. I need help for this problem.. if you have time, can you please take a look at this problem [link]http://stackoverflow.com/questions/17440716/how-to-check-all-edittext-empty-or-not-for-answer-that-i-put-in-list . thank you very much. – blackneko Jul 03 '13 at 06:06

2 Answers2

1

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();
                        }
                    }
blackneko
  • 91
  • 1
  • 12
0

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.

Waza_Be
  • 39,407
  • 49
  • 186
  • 260
  • Waza_Be, is it like this inside the postexecute `if(result==null) { Toast.makeText(ChatRoom.this, "Error", Toast.LENGTH_LONG).show(); } else{ friendLabel.setText(name); showMessage(message, false); }` or is it wrong? thank you very much – blackneko Jun 22 '13 at 10:37
  • if I put like that and initialize the name and message, in my chatroom will not appear anything, the result appear in logcat only. O.o – blackneko Jun 22 '13 at 10:46
  • I am pretty sure you are missing something, I have this code in many different apps and that's working fine. Check for your fields and some null values – Waza_Be Jun 22 '13 at 11:09
  • may I ask, what is the meaning for View.requestlayout in logcat? O.o is it something wrong with XML or .java? O.o thank you very much – blackneko Jun 22 '13 at 11:39
  • I have no idea... The important part is 2 lines above: CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views => That means that you needed to work in the UI Thread – Waza_Be Jun 22 '13 at 16:08
  • I persanally think you have an other problem that you should explain in a separate topic and consider this one as solved... Settings View and Text in on the postExecute should be trivial – Waza_Be Jun 22 '13 at 16:10