Only the original thread that created a view hierarchy can touch its views
translates to: Only the original thread that created a view hierarchy can touch its views
Reason:回调接口在子线程,并在回调接口对UI进行了操作
So, the above prompt will appear
Generally speaking, handle will be used to solve this problem
Different messages are passed on the corresponding callback interface according to the data returned by the network request
handler.sendMessage(msg);
Perform further operations based on the content of the message
private Handler handler = new Handler()
{
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what)
{
case 1:
...
break;
}
}
};
There is another solution that saves trouble
Use runOnUiThread() directly
But if you use it too much, the code will not be refreshing.
MainActivity.this.runOnUiThread(new Runnable() {
public void run() {
...
}
});
If it is a fragment:
getActivity.runOnUiThread(new Runnable() {
public void run() {
...
}
});