0

im having an issue with the navigation drawer. I am using eclipse and using the code it provides for the drawer. I have managed to add additional links to the drawer. When I click on them, the title (in the action bar) changes correctly, but the information doesnt change. I have created all the fragments inside the MainActivity.java file. Below is the code from my MainActivity.java...

MainActivity.java

public class MainActivity extends ActionBarActivity
    implements NavigationDrawerFragment.NavigationDrawerCallbacks {

/**
 * Fragment managing the behaviors, interactions and presentation of the navigation drawer.
 */
private NavigationDrawerFragment mNavigationDrawerFragment;

/**
 * Used to store the last screen title. For use in {@link #restoreActionBar()}.
 */
private CharSequence mTitle;

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

    mNavigationDrawerFragment = (NavigationDrawerFragment)
            getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
    mTitle = getTitle();

    // Set up the drawer.
    mNavigationDrawerFragment.setUp(
            R.id.navigation_drawer,
            (DrawerLayout) findViewById(R.id.drawer_layout));
}

@Override
public void onNavigationDrawerItemSelected(int position) {
    // update the main content by replacing fragments
    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction()
            .replace(R.id.container, SuikodenFragment.newInstance(position + 1))
            .replace(R.id.container, SuikodenIIFragment.newInstance(position + 1))
            .replace(R.id.container, SuikodenIIIFragment.newInstance(position + 1))
            .replace(R.id.container, SuikodenIVFragment.newInstance(position + 1))
            .replace(R.id.container, SuikodenVFragment.newInstance(position + 1))
            .commit();
}

public void onSectionAttached(int position) {
    switch (position) {
        case 1:
            mTitle = getString(R.string.title_suiko1);
            break;
        case 2:
            mTitle = getString(R.string.title_suiko2);
            break;
        case 3:
            mTitle = getString(R.string.title_suiko3);
            break;
        case 4:
            mTitle = getString(R.string.title_suiko4);
            break;
        case 5:
            mTitle = getString(R.string.title_suiko5);
            break;
    }
}

public void restoreActionBar() {
    ActionBar actionBar = getSupportActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
    actionBar.setDisplayShowTitleEnabled(true);
    actionBar.setTitle(mTitle);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    if (!mNavigationDrawerFragment.isDrawerOpen()) {
        // Only show items in the action bar relevant to this screen
        // if the drawer is not showing. Otherwise, let the drawer
        // decide what to show in the action bar.
        getMenuInflater().inflate(R.menu.main, menu);
        restoreActionBar();
        return true;
    }
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * Suikoden 1 Fragment Class
 */
public static class SuikodenFragment extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    private static final String ARG_SECTION_NUMBER = "section_number";

    /**
     * Returns a new instance of this fragment for the given section
     * number.
     */
    public static SuikodenFragment newInstance(int sectionNumber) {
        SuikodenFragment fragment = new SuikodenFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    public SuikodenFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.test_drawer_item1, container, false);
        return rootView;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        ((MainActivity) activity).onSectionAttached(
                getArguments().getInt(ARG_SECTION_NUMBER));
    }
}

/**
 * Suikoden 2 Fragment Class
 */
public static class SuikodenIIFragment extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    private static final String ARG_SECTION_NUMBER = "section_number";

    /**
     * Returns a new instance of this fragment for the given section
     * number.
     */
    public static SuikodenIIFragment newInstance(int sectionNumber) {
        SuikodenIIFragment fragment = new SuikodenIIFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    public SuikodenIIFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.test_drawer_item2, container, false);
        return rootView;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        ((MainActivity) activity).onSectionAttached(
                getArguments().getInt(ARG_SECTION_NUMBER));
    }
}

/**
 * Suikoden 3 Fragment Class
 */
public static class SuikodenIIIFragment extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    private static final String ARG_SECTION_NUMBER = "section_number";

    /**
     * Returns a new instance of this fragment for the given section
     * number.
     */
    public static SuikodenIIIFragment newInstance(int sectionNumber) {
        SuikodenIIIFragment fragment = new SuikodenIIIFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    public SuikodenIIIFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.test_drawer_item3, container, false);
        return rootView;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        ((MainActivity) activity).onSectionAttached(
                getArguments().getInt(ARG_SECTION_NUMBER));
    }
}

/**
 * Suikoden 4 Fragment Class
 */
public static class SuikodenIVFragment extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    private static final String ARG_SECTION_NUMBER = "section_number";

    /**
     * Returns a new instance of this fragment for the given section
     * number.
     */
    public static SuikodenIVFragment newInstance(int sectionNumber) {
        SuikodenIVFragment fragment = new SuikodenIVFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    public SuikodenIVFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.test_drawer_item4, container, false);
        return rootView;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        ((MainActivity) activity).onSectionAttached(
                getArguments().getInt(ARG_SECTION_NUMBER));
    }
}

/**
 * Suikoden 5 Fragment Class
 */
public static class SuikodenVFragment extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    private static final String ARG_SECTION_NUMBER = "section_number";

    /**
     * Returns a new instance of this fragment for the given section
     * number.
     */
    public static SuikodenVFragment newInstance(int sectionNumber) {
        SuikodenVFragment fragment = new SuikodenVFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    public SuikodenVFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.test_drawer_item5, container, false);
        return rootView;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        ((MainActivity) activity).onSectionAttached(
                getArguments().getInt(ARG_SECTION_NUMBER));
    }
}

I have checked this site many many times to come up with a solution and have found some 'solutions' that havent worked. For example, placing the onSectionAttached content inside the onNavigationDrawerItemSelected class. However, this doesnt work or crashes the app. I feel that I am close to figuring it out. Any help would be much appreciated! Thanks!

theCreed
  • 55
  • 11

2 Answers2

0

What is this code:

fragmentManager.beginTransaction()
        .replace(R.id.container, SuikodenFragment.newInstance(position + 1))
        .replace(R.id.container, SuikodenIIFragment.newInstance(position + 1))
        .replace(R.id.container, SuikodenIIIFragment.newInstance(position + 1))
        .replace(R.id.container, SuikodenIVFragment.newInstance(position + 1))
        .replace(R.id.container, SuikodenVFragment.newInstance(position + 1))
        .commit();

It should be like this:

switch(position){
case 0:
 fragmentManager.beginTransaction()
        .replace(R.id.container, SuikodenFragment.newInstance(position + 1)).commit();
//similar for others
}
Illegal Argument
  • 10,090
  • 2
  • 44
  • 61
  • wow! thank you so much. I am a beginner and that wrong code was something I was just trying. I appears that earlier in the day I was close to the code you have put up here. It works great! thanks you so much! – theCreed Jun 10 '14 at 14:54
0

I ran into the problem of the action bar not updating properly on Back presses using navigation drawer and also that the fragments just weren't working as intended, I posted a code example on this question here.

Android - How to change fragments in the Navigation Drawer

Community
  • 1
  • 1
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428