无法在android中动态添加TextView

时间:2011-12-25 17:25:51

标签: java android

有一种情况是,当用户按下按钮时,新的TextView将被添加到LinearLayout中。 这是我的测试用例:

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

}

但不幸的是,当按下按钮时它会抛出Exception

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:   访问http://10.0.2.2:9111/flight/CZ1310?date=2011-12-24将返回一个json字符串。

1 个答案:

答案 0 :(得分:1)

stacktrace说明了一切:你只能从UI线程更新UI。

使用runOnUiThread(),请参阅此link