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 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,413 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.