0

I have an app with a single activity that manages 5 fragments and I'm trying to code the logic part for each fragment and I don't know why is not working. I tried to put on "On Create" but the application crashes.

In oncreateView I can't code neither. because is a static method.

Here is my code structure:

**imports

public class MainActivity extends ActionBarActivity implements
        NavigationDrawerFragment.NavigationDrawerCallbacks {


private NavigationDrawerFragment mNavigationDrawerFragment;
private DrawerLayout mDrawerLayout;
private CharSequence mTitle;
private static final int FRAGMENT_MAIN_POSITION = 0;
private static final int FRAGMENT_1_POSITION = 1;
private static final int FRAGMENT_2_POSITION = 2;
private static final int FRAGMENT_3_POSITION = 3;
private static final int FRAGMENT_4_POSITION = 4;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mNavigationDrawerFragment = (NavigationDrawerFragment) getSupportFragmentManager()
            .findFragmentById(R.id.navigation_drawer);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mTitle = getTitle();
    // Establece el Drawer
    mNavigationDrawerFragment.setUp(R.id.navigation_drawer,
            (DrawerLayout)
                    findViewById(R.id.drawer_layout)
    );

} //End of onCreate!!

@Override
public void onNavigationDrawerItemSelected(int position) {
    // Actualiza el contenido principal reemplazando fragmentos
    FragmentManager fragmentManager = getSupportFragmentManager();
    switch (position) {
        case FRAGMENT_MAIN_POSITION:
            fragmentManager
                    .beginTransaction()
                    .replace(R.id.container,
                            FragmentMain.newInstance(position + 1)).commit();
            break;
        case FRAGMENT_1_POSITION:
            fragmentManager
                    .beginTransaction()
                    .replace(R.id.container,
                            Fragment_1.newInstance(position + 1)).commit();
            break;
        case FRAGMENT_2_POSITION:
            fragmentManager
                    .beginTransaction()
                    .replace(R.id.container,
                            Fragment_2.newInstance(position + 1)).commit();
            break;
        case FRAGMENT_3_POSITION:
            fragmentManager
                    .beginTransaction()
                    .replace(R.id.container,
                            Fragment_3.newInstance(position + 1)).commit();
            break;
        case FRAGMENT_4_POSITION:
            fragmentManager
                    .beginTransaction()
                    .replace(R.id.container,
                            Fragment_4.newInstance(position + 1)).commit();
            break;
    }

}

public void onSectionAttached(int number) {
    switch (number) {
        case 1:
            mTitle = "Title";
            break;
        case 2:
            mTitle = "Title";
            break;
        case 3:
            mTitle = "Title";
            break;
        case 4:
            mTitle = "Title";
            break;
        case 5:
            mTitle = "Title";
            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) {
   /**CODE */ 
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
 /** CODE */
}

/**
 * Fragmento Main
 */
public static class FragmentMain extends Fragment {

    // Argumento que representa el numero de seccion para este Fragment
    private static final String ARG_SECTION_NUMBER_1 = "section_number_1";

    // Regresa una nueva instancia de este Fragment
    public static FragmentMain newInstance(int sectionNumber) {
        FragmentMain fragment = new FragmentMain();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER_1, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    public FragmentMain() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container,
                false);
        // Aqui se intancia el contenido del Layout si se requiere
        return rootView;
    }

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


  public static class Fragment1 ...

  public static class Fragment2 ...

  public static class Fragment3 ...

What can I do? Thanks for your help.

makgyverzx
  • 39
  • 6
  • When I was using the drawerlayout created by Eclipse, I did the following: http://stackoverflow.com/questions/24006181/android-how-to-change-fragments-in-the-navigation-drawer/24042109#24042109 – EpicPandaForce Aug 07 '14 at 18:51
  • (also, a static class just makes the inner class accessible from the outside. It doesn't mean every of its methods are static. Meaning, onCreateView() for example is **not** static). – EpicPandaForce Aug 07 '14 at 18:52
  • 1
    Thanks @Zhuinden ! I solved it and now works like a charm. I had to make some changes on Manifest to fix getApplicationContext() due to I put each Fragment logic in a Java Class for each one. If you need solve this problem. Check this out: [link](http://stackoverflow.com/a/5114361/3642702) – makgyverzx Aug 11 '14 at 13:07

0 Answers0