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 Android Fragments Managing Fragments Fragment Flavors

Great job thanks! Now in NewsItemFragment.java, update the line that says int index = 0; to access the new bundle.

/NewsItemFragment.java:33: error: missing return statement } ^ 1 error

How do i correct this error? If i return int "index" it's giving me an error again.

public class MainActivity extends AppCompatActivity implements NewsFragment.NewsListener {
    // New tags for referring to fragments
    public static final String NEWS_FRAGMENT = "news_fragment";
    public static final String NEWSITEM_FRAGMENT = "newsitem_fragment";

    public void onCreate(Bundle savedInstanceState) {

        FragmentManager fragmentManager = getFragmentManager();

        NewsFragment savedFragment = (NewsFragment) fragmentManager.findFragmentById(R.id.placeholder);

        if (savedFragment == null) {
            NewsFragment newsFragment = new NewsFragment();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.add(R.id.placeholder, newsFragment);



    public void onNewsItemSelected(int index) {
        NewsItemFragment fragment = NewsItemFragment.createInstance(index);
        // TODO: how do we pass arguments to the new Fragment?

        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.placeholder, fragment);

public class NewsItemFragment extends Fragment {
    public static final String KEY_NEWS_ITEM_INDEX = "news_item_index";

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO:  Hmmm, how do we get this index?
        int index = 0;
        View view = inflater.inflate(R.layout.fragment_newsitem, container, false);
        return view;


public static NewsItemFragment createInstance(int index) {
        NewsItemFragment fragment = new NewsItemFragment();
       Bundle bundle = new Bundle();
        bundle.putInt("news_item_index", index);

        if (bundle != null) {
              index = bundle.getInt("news_item_index");
          return fragment;


    public void onStop() {
public class News {
    public static String[] headlines = new String[]{
      "Treehouse student develops #1 App in Google App Store",
      "Oracle and Google decide to hug it out",
      "Java8 coming to Android!  Is it too late?"

2 Answers

int index = getArguments().getInt( NewsItemFragment.KEY_NEWS_ITEM_INDEX);

thanks terrence,what about task 3, i am stuck again