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 Extending Thread

Eric De Wildt
PLUS
Eric De Wildt
Courses Plus Student 13,077 Points

Why do I get "Need inner class extending thread" error?

Why is the hint message showing "Bummer You should have a new inner class extending thread." when I already do?

MainActivity.java
public class MainActivity extends Activity {
    final TwitterClient twitterClient = new TwitterClient();
    private TwitterThread twitterThread;

    @Override
    public void onCreate(Bundle savedInstanceState) {
      setContentView(R.layout.activity_main);
      twitterThread = new TwitterThread(twitterClient);

      Thread t = twitterThread;
      t.setName("TwitterThread");
      t.start();
    }

   public class TwitterThread extends Thread {
     private TwitterClient tc;

    public TwitterThread(TwitterClient client){
       tc = client;
     }

    @Override
        public void run() {
          tc.update();
        }
  }
}

1 Answer

Seth Kroger
Seth Kroger
56,403 Points

In trying a couple of things, it looks like the challenge tester is expecting TwitterThread to have a default constructor. Because your's has a constructor that takes a TwitterThread, it no longer has one and the default has to be written back in.

public class MainActivity extends Activity {
    final TwitterClient twitterClient = new TwitterClient();

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

      TwitterThread t = new TwitterThread();
      t.setName("TwitterThread");
      t.start();
    }

    public class TwitterThread extends Thread {
        @Override
        public void run() {
          twitterClient.update();
        }      
    }
}

or using the rest of your code

// ...
   public class TwitterThread extends Thread {
     private TwitterClient tc;

    public TwitterThread() {
       super();  //  It's a good idea to call the superclass' constructor as well
    }

    public TwitterThread(TwitterClient client){
       super();
       tc = client;
     }

    @Override
     public void run() {
       tc.update();
     }
  }
Eric De Wildt
Eric De Wildt
Courses Plus Student 13,077 Points

Thanks Seth Kroger !

I knew I wasn't wrong and that it had to be something with what the tester actually looks for in the code. I have run into this in other courses on Treehouse.