setText不设置文本

时间:2012-01-23 12:31:45

标签: android settext

我有一个从网站上获取som数据的类。我跟着TheNewBoston's tutorial(我认为是147-149)并复制了他写的完全,但它对我不起作用。问题是setText。我尝试将tv.setText(returned)切换为tv.setText("Hello"),但不会改变。谁知道什么是错的?

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

    TextView tv= (TextView) findViewById(R.id.text1);
    GetMethodEx test = new GetMethodEx();
    String returned;

    try {
        returned = test.getInternetData();
        tv.setText(returned);
    } catch (Exception e) {
        e.printStackTrace();
    }

}

3 个答案:

答案 0 :(得分:1)

取代:

  try {
        returned = test.getInternetData();
        tv.setText(returned);
    } catch (Exception e) {
        e.printStackTrace();
    }

通过

  try {
        returned = test.getInternetData();
        tv.setText("returned");
    } catch (Exception e) {
        e.printStackTrace();
        tv.setText(e.getMessage());
    }
然后

setText将按您的要求设置文本!

答案 1 :(得分:1)

试试这个:

String returned = "some default string.";
try {
    returned = test.getInternetData();
} catch (Exception e) {
    e.printStackTrace();
}
tv.setText(returned);

这样,您就知道将使用数据调用setText,无论是否存在异常。

答案 2 :(得分:0)

runOnUiThread(new Runnable()
            {

                public void run()
                {

                tv.setText(returned.toString());

                }
            });