public class TestActivity extends Activity {
static final String TAG = "TestActivity";
private final String TEST_POLICY_URL = "...";
private static final String TEST_CONTENT_START_TAG = "<div class=\"adobt\">";
private static final String TEST_CONTENT_END_TAG = "</div>";
private String mTestContent = null;
@Override
public void onCreate(Bundle savedInstanceState) {
mTextPricyContent.setMovementMethod(LinkMovementMethod.getInstance());
loadTestPolicy(TEST_POLICY_URL);
}
private void loadTestPolicy(final String url) {
new Thread(new Runnable() {
@Override
public void run() {
mTestContent = getTestContent(url);
if(TextUtils.isEmpty(mTestContent)) {
mHandler.sendEmptyMessage(MSG_FAIL);
}
else {
mHandler.sendEmptyMessage(MSG_OK);
}
}
}).start();
}
static final int MSG_OK = 0;
static final int MSG_FAIL = 1;
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
switch (msg.what) {
case MSG_OK:
mTextTestTip.setVisibility(View.GONE);
Spanned span = Html.fromHtml(mTestContent);
mTextPricyContent.setVisibility(View.VISIBLE);
mTextPricyContent.setText(span);
break;
case MSG_FAIL:
mTextTestTip.setText(R.string.settings_policy_load_error);
break;
default:
break;
}
}
};
------解决方案--------------------
当你在UI线程中new一个线程出来之后,这里面的操作就不在UI线程中了,如果你要改变界面等内容,就必须要到UI线程中完成,所以就需要使用到handler