Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6965829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:06:43+00:00 2026-05-27T16:06:43+00:00

There is a case that when a user presses the button a new TextView

  • 0

There is a case that when a user presses the button a new TextView will be added into the LinearLayout.
Here is my test case:

package com.ggd543.json;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Text;

import java.io.IOException;

public class JsonActivity extends Activity {
    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TextView tv = (TextView) findViewById(R.id.text);
        // set a listener 
        setOnclickListener();

    }

    private void setOnclickListener() {
        ((Button) findViewById(R.id.click)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final TextView tv = new TextView(JsonActivity.this);
                LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.FILL_PARENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT
                );
                LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
                layout.addView(tv, p)
               // Start a thread to get some info and display it through Textview
                new Thread() {
                    public void run() {
                        HttpClient client = new DefaultHttpClient();
                        HttpGet req = new HttpGet("http://10.0.2.2:9111/flight/CZ1310?date=2011-12-24");
                        try {
                            HttpResponse res = client.execute(req);
                            String str = EntityUtils.toString(res.getEntity());
                             // set the content string 
                            tv.setText(str);
                        } catch (IOException e) {
                            e.printStackTrace();
                            tv.setText(e.getMessage());
                        }
                    }
                }.start();
            }
        });
    }

}

But unfortunately it throw an Exception when the button is pressed:

12-25 17:06:42.486: ERROR/AndroidRuntime(3541): Uncaught handler: thread Thread-8 exiting due to uncaught exception
12-25 17:06:42.486: ERROR/AndroidRuntime(3541): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
        at android.view.ViewRoot.checkThread(ViewRoot.java:2683)
        at android.view.ViewRoot.requestLayout(ViewRoot.java:557)
        at android.view.View.requestLayout(View.java:7918)
        at android.view.View.requestLayout(View.java:7918)
        at android.view.View.requestLayout(View.java:7918)
        at android.view.View.requestLayout(View.java:7918)
        at android.view.View.requestLayout(View.java:7918)
        at android.widget.TextView.checkForRelayout(TextView.java:5373)
        at android.widget.TextView.setText(TextView.java:2684)
        at android.widget.TextView.setText(TextView.java:2552)
        at android.widget.TextView.setText(TextView.java:2527)
        at com.ggd543.json.JsonActivity$1$1.run(JsonActivity.java:54)

PS:
To visit http://10.0.2.2:9111/flight/CZ1310?date=2011-12-24 will return a json string .

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T16:06:44+00:00Added an answer on May 27, 2026 at 4:06 pm

    The stacktrace says it all: you can update UI only from the UI thread.

    Use runOnUiThread() see this link.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following use case: There's a class called Template and with that
Are there rules of thumb that help determine which to use in what case?
Is there a reason I am missing that a block within a case statement
It's a simple case of a javascript that continuously asks are there yet? Like
Is there a way to enable exclusive checkouts on clear case? I want that
Ignoring the IE case, are there any other browsers that can't understand the application/xhtml+xml
I am trying to serialize my game data. In case the user presses the
I'm creating a program that will do a user-predefined number of tasks, say, 100.
The case: There is a .net application calling unmanaged C code. Used method for
Is there a case insensitive version of the :contains jQuery selector or should I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.