I’m working with an API. When I click the button in my app, it calls a synchronized void that requests data from the application. The data is then returned by CReceiver in the implemented method userInfo where I append the returned data to a TextView. I can System.out.println the information in userInfo and it works but if I try to append the TextView, I get a CalledFromWrongThreadException. What is the proper way to update the TextView with this information?
MainActivity
public class MainActivity extends Activity implements CReceiver {
private CSocket c_client = new CSocket(this);
private TextView textLog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnReqUser = (Button) findViewById(R.id.btnReqUser);
textLog = (TextView) findViewById(R.id.textLog);
btnReqUser.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
c_client.reqUser("roger");
}
});
}
@Override
public void userInfo(String firstName, String lastName, String address, String phone) {
// TODO Auto-generated method stub
textLog.append(firstName + " " + lastName + " " + address + " " + phone);
}
}
CSocket
public synchronized void reqUser(String userName) {
...
}
Since this is an
Activity, userunOnUiThread():