报错如下:
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
目前有两种方法
1.runOnUiThread
new Thread(new Runnable() {
@Override
public void run() {
final String string = httpClient.testHttp();
runOnUiThread(new Runnable() {
@Override
public void run() {
textView.setText(string);
}
});
}
}).start();
2.Handler
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == CAROUSE_OK) {
int position = msg.getData().getInt("position");
RequestOptions options = RequestOptions.bitmapTransform(ROUNDED_CORNERS_10);
carouseRadios.get(position).setChecked(true);
}
}
};
runnable = new Runnable() {
@Override
public void run() {
Message msg = new Message();
Bundle bundle = new Bundle();
bundle.putInt("imgId", carouseImagesInfo.get(point));//显示的图片id
bundle.putInt("position", point);//当前图片序号
msg.setData(bundle);
msg.what = Work3MainActivity.CAROUSE_OK;
handler.sendMessage(msg);
}
};
3.Handler post
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
ToastUtils.showShort(LoginActivity.this, "登录成功,uid = " + finalUid);
}
});