Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android Threads and Services Threads in Android Handle it!

A. L. Schreyer
A. L. Schreyer
1,354 Points

message.obj doesn't exist?

I keep getting the error that the compiler does not recognize message.obj in the following code:

String helloWorld = "Hello World"; Message message = Message.obtain(); message.obj = helloWorld; thread.mHandler.sendMessage(message);

Message.obj is defined in the Android documentation and I did everything the same way they did it in the video, so why is it saying it doesn't exist?

MainActivity.java
public class MainActivity extends Activity {
  Button button;
  TwitterClient twitterClient = new TwitterClient();

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

    final TwitterThread twitterThread = new TwitterThread();
    twitterThread.setName("TwitterThread");
    twitterThread.start();

    button = (Button) findViewById(R.id.update_button);
    button.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v){
        String hello = "hello";
        Message message = Message.obtain();
        message.obj = hello;
        thread.mHandler.sendMessage(message);
      }
    });
  }

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

  class TwitterHandler extends Handler {
    @Override
    public void handleMessage(Message msg){
      twitterClient.update();
    }
  }
}

2 Answers

Hi A. L.,

The problem with your code is that it doesn't do what the Challenge asks for. The Challenge doesn't want you to assign a string to message.obj, it just wants you to send off a message. Additionally, the Challenge is asking for you to assign mHandler inside the run function.

Hope that helps!

A. L. Schreyer
A. L. Schreyer
1,354 Points

The wording of the problem should be more clear because I thought the message was supposed to actually say something.

After you complete this Challenge, there's a button labelled Rate this Video that you can use to provide feedback. It's in the lower left under the chart showing your progress. If you'd like to have a reply, though, you can email concerns about a course directly to the course's teacher.