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 4022186
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:26:39+00:00 2026-05-20T10:26:39+00:00

This problem drives me crazy. I miss some basic but very important knowledge about

  • 0

This problem drives me crazy. I miss some basic but very important knowledge about how to handle long operations in a new thread created within an activity and how to modify view components like text and such after the long operation’s done.

Let me first show you the part of my code where this problem happens:

mProgressDialog = ProgressDialog.show(mContext, "Tripplanner", "please wait...", true, false);
connectAndGetRoute();


private void connectAndGetRoute(){

    new Thread(){
        @Override
        public void run() {
            try {
            if(!connectTo9292ov()) return;// conncetto9292ov() connects to a website, parses the reasult into an arraylist. The arraylist contains route.

            } catch(UnknownHostException e){
                Toast.makeText(mContext, "failed to connect to server", Toast.LENGTH_LONG).show();
            }catch (ClientProtocolException e) {
               Toast.makeText(mContext, "failed to connect to server", Toast.LENGTH_LONG).show();                   
        } catch (IOException e) {
                Toast.makeText(mContext, "failed to connect to server", Toast.LENGTH_LONG).show();
            }

            handler.post(runConnection);
        }

    }.start();

    handler = new Handler();
    runConnection = new Runnable(){
        @Override
        public void run() {
            mProgressDialog.dismiss();
            showOnScreen();
        }   
    };
}

and this is the error I get:

ERROR/WindowManager(8297): Activity mp.tripplanner.OvPlanner has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@46368a28 that was originally added here
ERROR/WindowManager(8297): android.view.WindowLeaked: Activity mp.tripplanner.OvPlanner has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@46368a28 that was originally added here
ERROR/WindowManager(8297): at android.view.ViewRoot.<init>(ViewRoot.java:251)
ERROR/WindowManager(8297): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
ERROR/WindowManager(8297): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
ERROR/WindowManager(8297): at android.view.Window$LocalWindowManager.addView(Window.java:424)
ERROR/WindowManager(8297): at android.app.Dialog.show(Dialog.java:241)
ERROR/WindowManager(8297): at android.app.ProgressDialog.show(ProgressDialog.java:107)
ERROR/WindowManager(8297): at android.app.ProgressDialog.show(ProgressDialog.java:95)
ERROR/WindowManager(8297): at mp.tripplanner.OvPlanner$3.onClick(OvPlanner.java:351)
ERROR/WindowManager(8297): at android.view.View.performClick(View.java:2408)
ERROR/WindowManager(8297): at android.view.View$PerformClick.run(View.java:8817)
ERROR/WindowManager(8297): at android.os.Handler.handleCallback(Handler.java:587)
ERROR/WindowManager(8297): at android.os.Handler.dispatchMessage(Handler.java:92)
ERROR/WindowManager(8297): at android.os.Looper.loop(Looper.java:144)
ERROR/WindowManager(8297): at android.app.ActivityThread.main(ActivityThread.java:4937)
ERROR/WindowManager(8297): at java.lang.reflect.Method.invokeNative(Native Method)
ERROR/WindowManager(8297): at java.lang.reflect.Method.invoke(Method.java:521)
ERROR/WindowManager(8297): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
ERROR/WindowManager(8297): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
ERROR/WindowManager(8297): at dalvik.system.NativeStart.main(Native Method)

But another error message is written to the log before the above one, which is:

ERROR/AndroidRuntime(8297): FATAL EXCEPTION: Thread-9
ERROR/AndroidRuntime(8297): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
ERROR/AndroidRuntime(8297): at android.os.Handler.<init>(Handler.java:121)
ERROR/AndroidRuntime(8297): at android.widget.Toast.<init>(Toast.java:68)
ERROR/AndroidRuntime(8297): at android.widget.Toast.makeText(Toast.java:231)
ERROR/AndroidRuntime(8297): at mp.tripplanner.OvPlanner$4.run(OvPlanner.java:371)

Thank you for your help.

  • 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-20T10:26:40+00:00Added an answer on May 20, 2026 at 10:26 am

    Your Handler needs to be created in your UI thread for it to be able to update the UI.
    I would then use the sendMessage method of the handler, rather than posting a runnable:

    private static final int HANDLER_MESSAGE_ERROR = 0;
    private static final int HANDLER_MESSAGE_COMPLETED = 1;
    ...
    private void connectAndGetRoute(){
        new Thread(){
            @Override
            public void run() {
                try {
                    if(!connectTo9292ov()) return;
    
                } catch(UnknownHostException e){
                    sendMessage(HANDLER_MESSAGE_ERROR);
                } catch (ClientProtocolException e) {
                    sendMessage(HANDLER_MESSAGE_ERROR);
                } catch (IOException e) {
                    sendMessage(HANDLER_MESSAGE_ERROR);
                } finally {
                    sendMessage(HANDLER_MESSAGE_COMPLETED);
                }
            }
            private void sendMessage(int what){
                Message msg = Message.obtain();
                msg.what = what;
                mHandler.sendMessage(msg);
            }
        }.start();
    
    }
    ...
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch(msg.what){
            case HANDLER_MESSAGE_ERROR:
                Toast.makeText(mContext, "failed to connect to server", Toast.LENGTH_LONG).show();
                break;
            case HANDLER_MESSAGE_COMPLETED:
                mProgressDialog.dismiss();
                showOnScreen();
                break;
            default:
                Log.w("MyTag","Warning: message type \""+msg.what+"\" not supported");
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm struggling with this probelm for few hours and it's drives me crazy. I
i used this method to handle shake event on my app , but i
I'm not certain when this problem started occuring, but it was approximately a few
since last post, did all the changes suggested but this problem still haunts me.
This problem crops up every now and then at work. Our build machine can
This problem occurred during daylight saving time change. After the change occurred, we've noticed
This problem has been solved thanks to your suggestions. See the bottom for details.
This problem has been kicking my butt for a few days now. I have
This problem involved me not knowing enough of C++. I am trying to access
This problem pertains to Java By using RandomAccessFile I intend to be able to

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.