1

Greetings,

I want to update the text in my application from my thread to be displayed in my activity.

The only way i know how to update an activity from a thread class (called in the activity) is by creating a static method that is called from thread like this:

Global.score++; //Global class with a score variable
Start.show(); //Start is the caller class 

And executed like this:

tvScore.setText(String.ValueOf(Global.score));                

And ofc change the modifier of the tvScore to static.

The only problem is that this code crashes my app, and i was wondering if there is some other way that i can display the score in my activity when something happens on a thread.

Thanks!

EDIT - LOGCAT SHOW
Not sure that this will help, but here is the logcat

07-21 02:15:10.215: E/AndroidRuntime(27608): FATAL EXCEPTION: Thread-91
07-21 02:15:10.215: E/AndroidRuntime(27608): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
07-21 02:15:10.215: E/AndroidRuntime(27608):    at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4039)
07-21 02:15:10.215: E/AndroidRuntime(27608):    at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:709)
07-21 02:15:10.215: E/AndroidRuntime(27608):    at android.view.View.requestLayout(View.java:12675)
07-21 02:15:10.215: E/AndroidRuntime(27608):    at android.view.View.requestLayout(View.java:12675)
07-21 02:15:10.215: E/AndroidRuntime(27608):    at android.view.View.requestLayout(View.java:12675)
07-21 02:15:10.215: E/AndroidRuntime(27608):    at android.view.View.requestLayout(View.java:12675)
07-21 02:15:10.215: E/AndroidRuntime(27608):    at android.view.View.requestLayout(View.java:12675) 
07-21 02:15:10.215: E/AndroidRuntime(27608):    at android.widget.TextView.checkForRelayout(TextView.java:6773)
07-21 02:15:10.215: E/AndroidRuntime(27608):    at android.widget.TextView.setText(TextView.java:3306)
07-21 02:15:10.215: E/AndroidRuntime(27608):    at android.widget.TextView.setText(TextView.java:3162)
07-21 02:15:10.215: E/AndroidRuntime(27608):    at android.widget.TextView.setText(TextView.java:3137)
07-21 02:15:10.215: E/AndroidRuntime(27608):    at com.example.fishtruck.Start.showLives(Start.java:123)
Pavle37
  • 571
  • 9
  • 24
  • Can you post a logcat if it's possible? – Lazy Jul 21 '14 at 00:09
  • sure give me 2-3 mins – Pavle37 Jul 21 '14 at 00:12
  • The exception tells you that only the Activity can change its own Views. You have to put the setText() code in the Activity that owns the tvScoreTextView. – joao2fast4u Jul 21 '14 at 00:54
  • I can read what it says, and the set text method is called from the activity where `findViewById` is called and that is the only place in the code where tvScore is defined. – Pavle37 Jul 21 '14 at 01:04
  • Then see the answer at http://stackoverflow.com/questions/5161951/android-only-the-original-thread-that-created-a-view-hierarchy-can-touch-its-vi – joao2fast4u Jul 21 '14 at 01:13

1 Answers1

0

This is how i resolved the issue of posting/updating text from a thread :

this.post(new Runnable() {

            @Override
            public void run() {
               textView.setText("something");
            }
        });

This code is located in the custom SurfaceView that calls the thread. And textView is just a reference passed from the Activity class.

Pavle37
  • 571
  • 9
  • 24