I am trying to pass my Main activity into a java class that has a running thread. When the thread is called I then have to reference some objects back in my activity class that do some work and update the UI. My code looks like this.
/**
*
*/
public class MainActivity extends Activity {
Person person;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
person = new Person();
}
public void updateInfo(String action) {
person.setAction(action);
//redraw the person to the gui
ImageView image = (ImageView) findViewById(R.id.action);
int id = getResources().getIdentifier(person.getCurrentAction(),"drawable", getPackageName());
image.setImageResource(id);
}
}
/**
*
*/
public class ThreadClass {
private MainActivity mainActivity;
public SocketClient(MainActivity mainActivity) {
this.mainActivity = mainActivity;
new MyThread().start();
}
private class MyThread extends Thread {
public void run() {
try {
while(!isInterrupted() {
mainActivity.updateInfo("Person is running");
}
}
}
}
}
When I do it this way I get the error that I am not updating the view in the correct thread. android.View.ViewRoot$CalledFromWrongThreadException. What am I doing wrong here? Also this code most likely will not compile its pseudo code because I had to take out all of the other logic.