Autore Topic: Problema drawer layout  (Letto 479 volte)

Offline swet90

  • Nuovo arrivato
  • *
  • Post: 21
  • Respect: 0
    • Mostra profilo
  • Dispositivo Android:
    Galaxy Nexus
  • Play Store ID:
    Davide Antonelli
  • Sistema operativo:
    Windows 8
Problema drawer layout
« il: 26 Luglio 2013, 16:04:47 CEST »
0
Ciao a tutti, ho implementato il navigation drawer nella mia app, ho solo il problema che quando clicco sull' icona del navigation drawer l' app va in crash indicando nullPointerException, di seguito allego il logcat e il codice:

Codice (Java): [Seleziona]
private DrawerLayout mDrawerLayout;
        private ActionBarDrawerToggle mDrawerToggle;

mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
                mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
                getActionBar().setDisplayHomeAsUpEnabled(true);
                getActionBar().setHomeButtonEnabled(true);
               
                ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(
                                this, mDrawerLayout, R.drawable.ic_drawer,
                                R.string.drawer_open, R.string.drawer_close){
                         
                            public void onDrawerClosed(View view) {
                                getActionBar().setTitle(getString(R.string.drawer_close));
                                invalidateOptionsMenu();}
                         
                            public void onDrawerOpened(View drawerView) {
                                getActionBar().setTitle(getString(R.string.drawer_open));
                                invalidateOptionsMenu();}
                        };

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Pass the event to ActionBarDrawerToggle, if it returns
        // true, then it has handled the app icon touch event
        if (mDrawerToggle.onOptionsItemSelected(item)) {
          return true;
        }
        // Handle your other action bar items...

        return super.onOptionsItemSelected(item);
    }
         
        @Override
        public void onConfigurationChanged(Configuration newConfig) {
                super.onConfigurationChanged(newConfig);
                mDrawerToggle.onConfigurationChanged(newConfig);
        }
ed ecco il log cat
Codice: [Seleziona]
07-26 16:03:31.181: W/dalvikvm(5629): threadid=1: thread exiting with uncaught exception (group=0x418c6700)
07-26 16:03:31.204: E/AndroidRuntime(5629): FATAL EXCEPTION: main
07-26 16:03:31.204: E/AndroidRuntime(5629): java.lang.NullPointerException
07-26 16:03:31.204: E/AndroidRuntime(5629):         at com.davide.palestrando.Home.onOptionsItemSelected(Home.java:241)
07-26 16:03:31.204: E/AndroidRuntime(5629):         at android.app.Activity.onMenuItemSelected(Activity.java:2566)
07-26 16:03:31.204: E/AndroidRuntime(5629):         at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:361)
07-26 16:03:31.204: E/AndroidRuntime(5629):         at com.android.internal.widget.ActionBarView$3.onClick(ActionBarView.java:171)
07-26 16:03:31.204: E/AndroidRuntime(5629):         at android.view.View.performClick(View.java:4240)
07-26 16:03:31.204: E/AndroidRuntime(5629):         at android.view.View$PerformClick.run(View.java:17721)
07-26 16:03:31.204: E/AndroidRuntime(5629):         at android.os.Handler.handleCallback(Handler.java:730)
07-26 16:03:31.204: E/AndroidRuntime(5629):         at android.os.Handler.dispatchMessage(Handler.java:92)
07-26 16:03:31.204: E/AndroidRuntime(5629):         at android.os.Looper.loop(Looper.java:137)
07-26 16:03:31.204: E/AndroidRuntime(5629):         at android.app.ActivityThread.main(ActivityThread.java:5103)
07-26 16:03:31.204: E/AndroidRuntime(5629):         at java.lang.reflect.Method.invokeNative(Native Method)
07-26 16:03:31.204: E/AndroidRuntime(5629):         at java.lang.reflect.Method.invoke(Method.java:525)
07-26 16:03:31.204: E/AndroidRuntime(5629):         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
07-26 16:03:31.204: E/AndroidRuntime(5629):         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-26 16:03:31.204: E/AndroidRuntime(5629):         at dalvik.system.NativeStart.main(Native Method)

e la riga incriminata sarebbe la 241 :

Codice (Java): [Seleziona]
@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Pass the event to ActionBarDrawerToggle, if it returns
        // true, then it has handled the app icon touch event
        if (mDrawerToggle.onOptionsItemSelected(item)) {
          return true;
        }
        // Handle your other action bar items...

        return super.onOptionsItemSelected(item);
    }
Sapete aiutarmi?
Grazie

Offline GabMarioPower

  • Moderatore globale
  • Utente senior
  • *****
  • Post: 606
  • Respect: +153
    • Github
    • Google+
    • gabrielemariotti
    • GabMarioPower
    • Mostra profilo
  • Play Store ID:
    GAB+MARIO+DEV
  • Sistema operativo:
    Ubuntu 14.04 , Win 10
Re:Problema drawer layout
« Risposta #1 il: 26 Luglio 2013, 17:23:03 CEST »
0
Codice (Java): [Seleziona]
ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle
In che metodo lo inizializzi?

Offline swet90

  • Nuovo arrivato
  • *
  • Post: 21
  • Respect: 0
    • Mostra profilo
  • Dispositivo Android:
    Galaxy Nexus
  • Play Store ID:
    Davide Antonelli
  • Sistema operativo:
    Windows 8
Re:Problema drawer layout
« Risposta #2 il: 26 Luglio 2013, 18:53:52 CEST »
0
nel metodo
Codice (Java): [Seleziona]
protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);