Reputation: 37328
I have added a TextView
to a RelativeLayout
that I already added a SurfaceView
to. I can get the TextView
to display text over my SurfaceView
.
rl = new RelativeLayout(this);
tv = new TextView(this);
tv.setText(Integer.toString(GameScreen.score));
tv.setTextSize(50);
tv.setPadding(390, 50, 0, 0);
tv.setTextColor(Color.BLACK);
rl.addView(renderView);
rl.addView(tv);
setContentView(rl);
Then in my GameScreen class's update method i put:
game.getTextView().setText(Integer.toString(score));
And it gives me the error:
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
What can I do to get around this ?
Upvotes: 0
Views: 1732
Reputation: 528
Use this peice of code.. Um sure it will help you out !
private void runThread() {
new Thread() {
public void run() {
try {
runOnUiThread(new Runnable() {
@Override
public void run() {
game.getTextView().setText(Integer.toString(score));
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}.start();
Let me know if it works! :)
Upvotes: 1
Reputation: 2790
Use the following which is the recommended way to update Widget
on UiThread
game.getTextView().post(new Runnable() {
@Override
public void run() {
game.getTextView().setText(Integer.toString(score));
}
});
Hope this helps.
Upvotes: 3
Reputation: 5385
run your code in the UI thread. You cannot do UI operations from a worker thread.
your_context.runOnUiThread(new Runnable() {
public void run(){
game.getTextView().setText(Integer.toString(score));
}
});
Upvotes: 1