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 Tablet Time Enter the Grid

Joel Afamefune
Joel Afamefune
4,730 Points

Variable saveFragment is already defined in the scope

I got an error; variable "savedFragment" is already defined in the scope. Its weird because it doesn't show up on the video. I tried renaming the list fragment and grid fragment. That gets rid of the error, but the grid view doesn't show up on the tablet. Any help is appreciated.

MainActivity.java:

package com.jafamefune.smellslikebakin.ui;

import android.os.Bundle; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.support.v7.app.AppCompatActivity; import android.widget.Toast;

import com.jafamefune.smellslikebakin.R;

public class MainActivity extends AppCompatActivity implements ListFragment.OnRecipeSelectedInterface, GridFragment.OnRecipeSelectedInterface { public static final String LIST_FRAGMENT = "list_fragment"; public static final String VIEWPAGER_FRAGMENT = "viewpager_fragment";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    boolean isTablet = getResources().getBoolean(R.bool.is_tablet);
    if (!isTablet) {
        //takes a resource id to get a fragment and saves it to savedFragment
        ListFragment savedFragment = (ListFragment) getSupportFragmentManager().findFragmentByTag(LIST_FRAGMENT);
        if (savedFragment == null) {
            ListFragment fragment = new ListFragment();
            FragmentManager fragmentManager = getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.add(R.id.placeholder, fragment, LIST_FRAGMENT);
            fragmentTransaction.commit();
        }
        else {
            GridFragment savedFragment = (GridFragment) getSupportFragmentManager().findFragmentByTag(LIST_FRAGMENT);
            if (savedFragment == null) {
                GridFragment fragment = new GridFragment();
                FragmentManager fragmentManager = getSupportFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.add(R.id.placeholder, fragment, LIST_FRAGMENT);
                fragmentTransaction.commit();
            }
        }
    }
}

@Override
public void onListRecipeSelected(int index) {
    ViewPagerFragment listFragment = new ViewPagerFragment();
    Bundle bundle = new Bundle();
    bundle.putInt(ViewPagerFragment.KEY_RECIPE_INDEX, index);
    listFragment.setArguments(bundle);
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.placeholder, listFragment, VIEWPAGER_FRAGMENT);
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();
}

@Override
public void onGridRecipeSelected(int index) {
}

}

1 Answer

Kourosh Raeen
Kourosh Raeen
23,733 Points

You have a misplaced closing brace. The else block is for the outermost if statement:

if (!isTablet) {
    //takes a resource id to get a fragment and saves it to savedFragment
    ListFragment savedFragment = (ListFragment) getSupportFragmentManager()
       .findFragmentByTag(LIST_FRAGMENT);
    if (savedFragment == null) {
        ListFragment fragment = new ListFragment();
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.placeholder, fragment, LIST_FRAGMENT);
        fragmentTransaction.commit();
    }
} else {
    GridFragment savedFragment = (GridFragment) getSupportFragmentManager()
        .findFragmentByTag(LIST_FRAGMENT);
    if (savedFragment == null) {
        GridFragment fragment = new GridFragment();
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.placeholder, fragment, LIST_FRAGMENT);
        fragmentTransaction.commit();
    }
}