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

Nancy Melucci
PLUS
Nancy Melucci
Courses Plus Student 36,143 Points

Problems with sendMessage(msg) in code challenge.

The error I get is: "Don't forget to override 'onStartCommand(Intent intent, int flags, int startId)' and make sure you're calling 'sendMessage(Message msg)' on the Handler from your TwitterThread instance."

But I've overridden onStartCommand and I am calling the sendMessage method.

Where should I be looking to fix this?

thanks.

TwitterService.java
public class TwitterService extends Service {
  TwitterClient twitterClient = new TwitterClient();    

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

    }

      @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();
    }

  } 


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

  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){

      }
    });
  }
}

3 Answers

Kourosh Raeen
Kourosh Raeen
23,733 Points

I see a couple of problems in the code:

1) sendMessage(msg) should be sendMessage(message) since in the previous line you are calling the Message instance message and not msg.

2) In the line TwitterHandler.sendMessage(msg); you are calling sendMessage() on the class itself. You should call it on an instance of TwitterHandler. We already have the instance in the TwitterThread inner class. In that class there is a variable called twitterHandler. To get to it you need to use thread variable which is an instance of TwitterThread. Also, I would suggest defining your thread variable at the top of the TwitterService class and then initializing it inside the onCreate() method. My code looks like this:

public class TwitterService extends Service {
  TwitterClient twitterClient = new TwitterClient();    

  TwitterThread twitterThread;

  @Override 
  public void onCreate() {
    twitterThread = new TwitterThread();
    twitterThread.start();
  }

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

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


  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();
    }
  }
}

cough cough, not void but int onStartCommand ?

and where is the return statement, dude i really need help

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

Nancy Melucci
PLUS
Nancy Melucci
Courses Plus Student 36,143 Points

I have to apologize. I was literally falling asleep while working on the app last night. I didn't even see the MainActivity tab and I kept working through and lost track of which sections I completed.

I am going to skip this challenge for now and keep working on the app. I'll come back to it in a few days. I have no idea what I wrote.

I work full time now so these things happen when I work on Treehouse projects from time to time. Thanks for your patience.

Kourosh Raeen
Kourosh Raeen
23,733 Points

Hi Nancy - No worries! I'll be glad to help with any questions you have. Happy coding!