Partage

Problème avec setUp dans NavigationDrawerFragment

Sujet résolu
3 octobre 2015 à 13:35:38

Bonjour,

/*Update

Je viens de télécharger Android Studio 1.4 avec les nouveau templates ;)

Donc problème résolu

*/

Je suis en train de faire une application pour Android et j'ai besoin d'un Navigation drawer, je suis donc parti du template fournie par Google dans Android Studio. De base il n'y a qu'un item, j'en ai donc rajoutés. 

Mon problème et que tout se compile nickel, mais dès que l'application se lance j'obtient ce rapport d'erreur : 

10-03 13:19:41.095 29912-29912/com.nickLPro.nicolas.ConsoRc E/AndroidRuntime: FATAL EXCEPTION: main
10-03 13:19:41.095 29912-29912/com.nickLPro.nicolas.ConsoRc E/AndroidRuntime: Process: com.nickLPro.nicolas.ConsoRc, PID: 29912
10-03 13:19:41.095 29912-29912/com.nickLPro.nicolas.ConsoRc E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nickLPro.nicolas.ConsoRc/com.nickLPro.nicolas.ConsoRc.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.nickLPro.nicolas.ConsoRc.NavigationDrawerFragment.setUp(int, android.support.v4.widget.DrawerLayout)' on a null object reference
10-03 13:19:41.095 29912-29912/com.nickLPro.nicolas.ConsoRc E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
10-03 13:19:41.095 29912-29912/com.nickLPro.nicolas.ConsoRc E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2367)
10-03 13:19:41.095 29912-29912/com.nickLPro.nicolas.ConsoRc E/AndroidRuntime:     at android.app.ActivityThread.access$800(ActivityThread.java:148)
10-03 13:19:41.095 29912-29912/com.nickLPro.nicolas.ConsoRc E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)
10-03 13:19:41.095 29912-29912/com.nickLPro.nicolas.ConsoRc E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
10-03 13:19:41.095 29912-29912/com.nickLPro.nicolas.ConsoRc E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:135)
10-03 13:19:41.095 29912-29912/com.nickLPro.nicolas.ConsoRc E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5274)
10-03 13:19:41.095 29912-29912/com.nickLPro.nicolas.ConsoRc E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
10-03 13:19:41.095 29912-29912/com.nickLPro.nicolas.ConsoRc E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)
10-03 13:19:41.095 29912-29912/com.nickLPro.nicolas.ConsoRc E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:909)
10-03 13:19:41.095 29912-29912/com.nickLPro.nicolas.ConsoRc E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704)
10-03 13:19:41.095 29912-29912/com.nickLPro.nicolas.ConsoRc E/AndroidRuntime:  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.nickLPro.nicolas.ConsoRc.NavigationDrawerFragment.setUp(int, android.support.v4.widget.DrawerLayout)' on a null object reference
10-03 13:19:41.095 29912-29912/com.nickLPro.nicolas.ConsoRc E/AndroidRuntime:     at com.nickLPro.nicolas.ConsoRc.MainActivity.onCreate(MainActivity.java:54)
10-03 13:19:41.095 29912-29912/com.nickLPro.nicolas.ConsoRc E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:5977)
10-03 13:19:41.095 29912-29912/com.nickLPro.nicolas.ConsoRc E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
10-03 13:19:41.095 29912-29912/com.nickLPro.nicolas.ConsoRc E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2258)
10-03 13:19:41.095 29912-29912/com.nickLPro.nicolas.ConsoRc E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2367) 
10-03 13:19:41.095 29912-29912/com.nickLPro.nicolas.ConsoRc E/AndroidRuntime:     at android.app.ActivityThread.access$800(ActivityThread.java:148) 
10-03 13:19:41.095 29912-29912/com.nickLPro.nicolas.ConsoRc E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283) 
10-03 13:19:41.095 29912-29912/com.nickLPro.nicolas.ConsoRc E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102) 
10-03 13:19:41.095 29912-29912/com.nickLPro.nicolas.ConsoRc E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:135) 
10-03 13:19:41.095 29912-29912/com.nickLPro.nicolas.ConsoRc E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5274) 
10-03 13:19:41.095 29912-29912/com.nickLPro.nicolas.ConsoRc E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method) 
10-03 13:19:41.095 29912-29912/com.nickLPro.nicolas.ConsoRc E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372) 
10-03 13:19:41.095 29912-29912/com.nickLPro.nicolas.ConsoRc E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:909) 
10-03 13:19:41.095 29912-29912/com.nickLPro.nicolas.ConsoRc E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704) 

Voici le setUp placé dans mon fichier NavigationDrawerFragment :

    public void setUp(int fragmentId, DrawerLayout drawerLayout) {
        mFragmentContainerView = getActivity().findViewById(fragmentId);
        mDrawerLayout = drawerLayout;

        // set a custom shadow that overlays the main content when the drawer opens
        mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
        // set up the drawer's list view with items and click listener

        ActionBar actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setHomeButtonEnabled(true);

        // ActionBarDrawerToggle ties together the the proper interactions
        // between the navigation drawer and the action bar app icon.
        mDrawerToggle = new ActionBarDrawerToggle(
                getActivity(),                    /* host Activity */
                mDrawerLayout,                    /* DrawerLayout object */
                R.drawable.ic_drawer,             /* nav drawer image to replace 'Up' caret */
                R.string.navigation_drawer_open,  /* "open drawer" description for accessibility */
                R.string.navigation_drawer_close  /* "close drawer" description for accessibility */
        ) {
            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
                if (!isAdded()) {
                    return;
                }

                getActivity().supportInvalidateOptionsMenu(); // calls onPrepareOptionsMenu()
            }

            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                if (!isAdded()) {
                    return;
                }

                if (!mUserLearnedDrawer) {
                    // The user manually opened the drawer; store this flag to prevent auto-showing
                    // the navigation drawer automatically in the future.
                    mUserLearnedDrawer = true;
                    SharedPreferences sp = PreferenceManager
                            .getDefaultSharedPreferences(getActivity());
                    sp.edit().putBoolean(PREF_USER_LEARNED_DRAWER, true).apply();
                }

                getActivity().supportInvalidateOptionsMenu(); // calls onPrepareOptionsMenu()
            }
        };

        // If the user hasn't 'learned' about the drawer, open it to introduce them to the drawer,
        // per the navigation drawer design guidelines.
        if (!mUserLearnedDrawer && !mFromSavedInstanceState) {
            mDrawerLayout.openDrawer(mFragmentContainerView);
        }

        // Defer code dependent on restoration of previous instance state.
        mDrawerLayout.post(new Runnable() {
            @Override
            public void run() {
                mDrawerToggle.syncState();
            }
        });

        mDrawerLayout.setDrawerListener(mDrawerToggle);
    }

Et le onCreate + onNavigationDrawerItemSelected + onSectionAttached dans mon fichier Main : 

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

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

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

        mNavigationDrawerFragment1 = (NavigationDrawerFragment)
                getSupportFragmentManager().findFragmentById(R.id.navigation_drawer_nitro);
        mTitle1 = getTitle();

        // Set up the drawer.
        mNavigationDrawerFragment1.setUp(
                R.id.navigation_drawer_nitro,
                (DrawerLayout) findViewById(R.id.drawer_layout_nitro));

        mNavigationDrawerFragment2 = (NavigationDrawerFragment)
                getSupportFragmentManager().findFragmentById(R.id.navigation_drawer_elec);
        mTitle2 = getTitle();

        // Set up the drawer.
        mNavigationDrawerFragment2.setUp(
                R.id.navigation_drawer_elec,
                (DrawerLayout) findViewById(R.id.drawer_layout_elec));


    }

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

        FragmentManager fragmentManager1 = getSupportFragmentManager();
        fragmentManager1.beginTransaction()
                .replace(R.id.container_nitro, PlaceholderFragment.newInstance(position + 1))
                .commit();

        FragmentManager fragmentManager2 = getSupportFragmentManager();
        fragmentManager2.beginTransaction()
                .replace(R.id.container_elec, PlaceholderFragment.newInstance(position + 1))
                .commit();

    }

    public void onSectionAttached(int number) {
        switch (number) {
            case 1:
                mTitle = getString(R.string.title_section1);
                break;
            case 2:
                mTitle1 = getString(R.string.title_section2);
                break;
            case 3:
                mTitle2 = getString(R.string.title_section3);
                break;
        }
    }



Si quelqu'un peut m'aider merci, parce que ca fait une semaine que je cherche et que j'essaie de modifier le setUp et de voir d'où vient le problème, mais je n'y arrive pas...

Merci

Nico Loriot

-
Edité par nicoloriot 3 octobre 2015 à 14:30:06

Vous êtes demandeur·se d'emploi ?
Sans diplôme post-bac ?

Devenez Développeur·se web junior

Je postule
Formation
courte
Financée
à 100%

Problème avec setUp dans NavigationDrawerFragment

× Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
× Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
  • Editeur
  • Markdown