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 Intents and Broadcast Receivers For All Intents and Purposes Intentional Practice

intents and broadcasts receivers

Challenge Task 1 of 4

The app below grants the user the power to travel through time! All they need to do is enter a year and tap on the "Go!" button to travel through time.....in the context of a second Activity called TimeTravelActivity. In the onClick() method for the button, add an Intent that we can use to start TimeTravelActivity.

MainActivity.java
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {

  public Button goButton;
  public EditText yearField;

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

    goButton = (Button)findViewById(R.id.goButton);
    yearField = (EditText)findViewById(R.id.yearField);

    goButton.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        Intent intent = new Intent();
        // Add your code in here!

      }
    });
  }
}
TimeTravelActivity.java
import android.os.Bundle;
import android.view.View;

public class TimeTravelActivity extends Activity {

  public String targetYear;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_time_travel);


  }
}
Simon Coates
Simon Coates
28,694 Points

there's some code at https://teamtreehouse.com/community/i-have-key-and-string-parameters-why-the-error that can be used to pass tasks 1-3. (it's been a month or two since i did the android stuff, so I'd need to revisit my notes if you require an explanation.). UPDATE: i posted complete code at that link. But the process is just to create an intent, put an extra on that intent and start the activity (using the intent). In the resultant activity, you just need to retrieve the intent and get the value off it.

1 Answer

ofir moalmi
ofir moalmi
2,161 Points

your intent has no parameters, you arent giving it details such as what activity to start and the current instance, your intent should look like this:

Intent intent = new Intent(MainActivity.this, TimeTravelActivity.class);

thanks ofir now stuck on task 2

thanks simon