Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Threads and Services Introducing Services Services Are Not Threads

The code does not work. The UI hangs up.

I really don't know why this happens. I think the code is exactly the same as what the video instructs. I am not a beginner programmer by the way. The main UI thread hangs up. I have included the Service in Manifest file. Do you have a working code uploaded somewhere like GitHub so I can run to check? The code up to this video lesson is fine.

Ben Deitch
Ben Deitch
Treehouse Teacher

On the 'Downloads' tab of that video you can download the project to compare with :)

2 Answers

Sachin Jayaram
Sachin Jayaram
5,461 Points

Hi Make sure in your MainActivity when you start the service you use startService(intent); and not startActivity(intent); Its a simple error anyone can make.

Piyush Ravi
Piyush Ravi
2,217 Points

I was facing the same issue. However when I closely inspected I found I was assigning the value and before starting the thread and while loop was something like this.

DownloadThread thread = new DownloadThread();
        handler = thread.mHandler; // mHandler not instantiated as thread has not started
        thread.setName("download thread");
        thread.start();
        while(handler == null) { // handler will always be null
           // this goes into infinite loop
        }
      // correct solution below

        thread.setName("download thread");
        thread.start();
        while(thread.mHandler == null) {

        }
        handler = thread.mHandler;