I’ve been struggling with this issue for a while now, and none of the other solutions I found on stackoverflow have solved my problem.
My UI thread has a handler:
//Global declarations
private UIHandler mHandler;
class UIHandler extends Handler {
TextView actionTV, objectTV;
public UIHandler(TextView t1,TextView t2){
actionTV = t1;
objectTV = t2;
}
@Override
public void handleMessage(Message msg) {
// a message is received; update UI text view
actionTV.setVisibility(View.VISIBLE); //Throws "CalledFromWrongThreadExc"
objectTV.setVisibility(View.VISIBLE);
System.out.println("Received Message");
}
}
//Inside of onCreate()
actionText = (TextView) findViewById(R.id.diceAction);
objectText = (TextView) findViewById(R.id.diceObject);
mHandler = new UIHandler(actionText,objectText);
//Inside of onClick
renderer.rollDice(mHandler);
actionText.setVisibility(View.GONE);
objectText.setVisibility(View.GONE);
And the handler receives a message from my openGL rendering thread. The message passes successfully, as I have tested that out.
I am getting a “CalledFromWrongThread” error on the line “action.setVisibility(View.VISIBLE)”. I thought passing the TextView into the Handler in onCreate() would solve the problem, but it hasn’t. If someone can point out where I went wrong, I’d greatly appreciate it.
In this question, it appears you must ‘invalidate’ your textviews after modifying them. help with Handler class to update UI – Android
Also usually ui actions are not done from the handler thread, they are done with a runnable that the handler posts. The runnable would be in the Activity thread. See if these suggestions help, or get you on the right track.