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 Creating a Service

Nathan Pickard
Nathan Pickard
10,950 Points

Task 2 of 3 Handler help

After trying a few bits of code, I can't seem to pass this challenge. It looks like I'm not implementing the twitterHandler correctly. Any help with this? Thanks!

TwitterService.java
public class TwitterService extends Service {

  //private TwitterHandler mHandler;
  TwitterClient twitterClient = new TwitterClient();  

  @Override
  public void onCreate() {
    TwitterThread thread = new TwitterThread();
    thread.setName("TwitterThread");
    thread.start();   
  } 

  @Override
  public void onStartCommand(Intent intent, int flags, int startId) {
    Message message = Message.obtain();
    twitterHandler.sendMessage(message);
  }

  @Override
  public IBinder onBind(Intent intent) {
    return null;
  }  

  class TwitterThread extends Thread {
    TwitterHandler twitterHandler;
    @Override
    public void run() {
      Looper.prepare();
      twitterHandler = new TwitterHandler();
      Looper.loop();
    }
  }    

  class TwitterHandler extends Handler {
    @Override
    public void handleMessage(Message msg) {
      twitterClient.update();
    }
  }
}
MainActivity.java
public class MainActivity extends Activity {
  Button button;   

  @Override
  public void onCreate(Bundle savedInstanceState) {
    setContentView(R.layout.activity_main);

    button = (Button) findViewById(R.id.update_button);
    button.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v){

      }
    });
  }
}

2 Answers

Ben Deitch
STAFF
Ben Deitch
Treehouse Teacher

Hey Nathan!

You can't use 'twitterHandler' in onStartCommand. It only exists in the TwitterThread class. To call 'twitterHandler.sendMsg()', you'll need to start with an instance of TwitterThread:

twitterThread.twitterHandler.sendMsg(message);
Nathan Pickard
Nathan Pickard
10,950 Points

Hey Ben!

I changed onStartCommand to the following:

  @Override
  public void onStartCommand(Intent intent, int flags, int startId) {
    TwitterThread twitterThread = new TwitterThread();
    Message message = Message.obtain();
    twitterThread.twitterHandler.sendMessage(message);
  }

However, I'm getting a NullPointerException. Perhaps I haven't started the instance of TwitterThread correctly? Thanks!

Ben Deitch
Ben Deitch
Treehouse Teacher

You need to start your thread in the onCreate method, and then use it in onStartCommand. 'twitterThread' should be a field in the TwitterService class.

ben deitch you are not teaching us , rather your are confusing us, in your videos you are saying something, when it comes to tasks, you are asking the complete opposite, in this this task what do you really require, cant seem to figure it out also, can you please elaborate, clearly. rather than confuse us the way you are doing