Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7081517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:55:51+00:00 2026-05-28T06:55:51+00:00

I cant figure out why i cant set text to my textView tv. getting:

  • 0

I cant figure out why i cant set text to my textView tv.
getting:

E/AndroidRuntime(686): android.view.ViewRoot$CalledFromWrongThreadException:
Only the original thread that created a view hierarchy can touch its views.

I tried many ways to make it right.
As you can see i tried Handler because i had the same problem with toasts. Now toast works but setText doesnt :((
Please someone help me, how should i configure this handler?

public class calculate extends Activity implements OnClickListener {
    private myService myService; //bound service instance
    private boolean serviceStarted;
    View show_map;
    View data;
    View start;
    View stop;
    public TextView tv;
    private Location loc;
    private boolean initiated=false;
    private float distance=0;
    UIHandler uiHandler;
    route_calc rc;

public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.calculate);
    tv=(TextView)findViewById(R.id.textView1);
    show_map=findViewById(R.id.button1);
    show_map.setOnClickListener(this);
    data=findViewById(R.id.button2);
    data.setOnClickListener(this);
    start=findViewById(R.id.button3);
    start.setOnClickListener(this);
    stop=findViewById(R.id.button4);
    stop.setVisibility(View.INVISIBLE);
    stop.setOnClickListener(this);
    HandlerThread uiThread = new HandlerThread("UIHandler");
    uiThread.start();
    uiHandler = new UIHandler( uiThread.getLooper());

}

public void onDestroy(){
    super.onDestroy();
}


@Override
public void onClick(View v) {
    Intent i;
    switch(v.getId()){
    case R.id.button1:
        i=new Intent(this,Map.class);
        startActivity(i);
        break;
    case R.id.button2:
        i=new Intent(this,data.class);
        startActivity(i);
        break;
    case R.id.button3:
        startService();

        break;
    case R.id.button4:
        stopService();
        break;
    }

}

//connection between this activity and service myService
ServiceConnection myServConn = new ServiceConnection() {
    @Override
    public void onServiceDisconnected(ComponentName arg0) {
        myService = null;
    }
    @Override
    public void onServiceConnected(ComponentName arg0, IBinder binder) {
        myService = ((myService.MyBinder)binder).getMyService();
    }
};

private void startService() {
    Intent intent = new Intent(this, myService.class); 
    startService(intent);
    //Bind MyService here
    bindService(intent, myServConn, BIND_AUTO_CREATE);
    stop.setVisibility(View.VISIBLE);
    serviceStarted = true;
    rc = new route_calc();
    rc.start();
}

private void stopService() {
    if(serviceStarted) {
        Intent intent = new Intent(this, myService.class);
        //Unbind MyService here
        unbindService(myServConn);
        stopService(intent);
        stop.setVisibility(View.INVISIBLE);

        serviceStarted = false;
    }
}

void showToast(String s){
    handleUIRequest(s);
}

void setText(){
    handleUISetText();
}

class route_calc extends Thread{
    Location begin;
    public void run() {
        float temp;


        while(!initiated){
            try{

                loc=myService.getLocation();

            }
            catch(Exception e){

            }

            if(loc!=null){
                begin=loc;
                initiated=true;
                showToast("zadzialalo");
            }

        }
        while(true){
            loc=myService.getLocation();
            temp=begin.distanceTo(loc);
            distance=distance+temp;
            tv.setText("przejechales "+distance+" m");
            System.err.println(distance);
            begin=loc;
            try {
                this.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }

    }

}


private final class UIHandler extends Handler
{
    public static final int DISPLAY_UI_TOAST = 0;
    public static final int TV_SET_TEXT = 1;

    public UIHandler(Looper looper)
    {
        super(looper);
    }


    public void handleMessage(Message msg)
    {
        switch(msg.what)
        {
        case UIHandler.DISPLAY_UI_TOAST:
        {
            Context context = getApplicationContext();
            Toast t = Toast.makeText(context, (String)msg.obj, Toast.LENGTH_LONG);
            t.show();
        }

        case UIHandler.TV_SET_TEXT:
        {

            tv.setText("przejechałeś "+distance+" m");
        }
        default:
            break;
        }
    }
}

protected void handleUIRequest(String message)
{
    Message msg = uiHandler.obtainMessage(UIHandler.DISPLAY_UI_TOAST);
    msg.obj = message;
    uiHandler.sendMessage(msg);
}

protected void handleUISetText(){
    Message msg=uiHandler.obtainMessage(UIHandler.TV_SET_TEXT);
    uiHandler.sendMessage(msg);
}

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T06:55:52+00:00Added an answer on May 28, 2026 at 6:55 am

    It seems like you put your entire Activity here, and that it also includes a service, and you didn’t try to narrow down your problem.

    in your route_calc thread you call showToast, this is probably one of your problems, you should call showToast (or any other UI function) from your Handler.

    Something like this:

    Do anything you want on your thread:

       new Thread(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        try
                        {
                            someHeavyStuffHere(); //Big calculations or file download here.
                            handler.sendEmptyMessage(SUCCESS);
                        }
                        catch (Exception e)
                        {
                            handler.sendEmptyMessage(FAILURE);
                        }
                    }
                }).start();
    

    When your data is ready, tell the handler to put it in a view and show it:

    protected Handler handler = new Handler()
        {
            @Override
            public void handleMessage(Message msg)
            {
                if (msg.what == SUCCESS)
                {
                    setCalculatedDataToaView(); // the data you calculated from your thread can now be shown in one of your views.
                }
                else if (msg.what == FAILURE)
                {
                    errorHandlerHere();//could be your toasts or any other error handling...
                }
            }
        };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am getting segmentation fault in this code but i cant figure out why.
I can't figure out how to set an NSDateFormatter for a static text table
I can't figure out how to tell the accordioncontainer to set height of its
I just can't figure it out how to set custom validator messages in Zend_Form
I cant figure out how to get lua to do any common timing tricks,
I want to do something but cant figure out how to do this (i
This seems in my head like it should work but I cant figure out
Maybe I missed something, but I cant figure out why Visual Studio 2008 isn't
Total newbie question, but I cant figure out what im doing wrong. I want
I cant really figure out whats wrong with this. I used to write the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.