2016-04-14 2 views
-2

Ich möchte eine Navigationsschublade in meiner App importieren. I-Code es in Fragmente, aber meine app stürzt auf Start und ich erhalte den folgenden Fehler:Brauchen Sie Hilfe bei Fehler mit der Navigationsschublade

Error: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.design.widget.NavigationView.setNavigationItemSelectedListener(android.support.design.widget.NavigationView$OnNavigationItemSelectedListener)' on a null object reference

Code:

import android.content.res.Configuration; 
import android.os.Bundle; 
import android.support.design.widget.NavigationView; 
import android.support.v4.widget.DrawerLayout; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.view.GravityCompat; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.MenuItem; 
import android.support.v4.app.Fragment; 
import android.support.v7.app.ActionBarDrawerToggle; 

public class NavigationDrawer extends AppCompatActivity { 
    private DrawerLayout NavDrawer; 
    private Toolbar toolbar; 


    private ActionBarDrawerToggle setupDrawerToggle() { 
     return new ActionBarDrawerToggle(this, NavDrawer, toolbar, R.string.drawer_open, R.string.drawer_close); 
    } 

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


     toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 


     NavDrawer = (DrawerLayout) findViewById(R.id.drawer_layout); 

     NavigationView nvDrawer = (NavigationView) findViewById(R.id.nvView); 

     setupDrawerContent(nvDrawer); 

     ActionBarDrawerToggle toggle = setupDrawerToggle(); 

     NavDrawer.addDrawerListener(toggle); 
    } 

    private void setupDrawerContent(NavigationView navigationView) { 
     navigationView.setNavigationItemSelectedListener(
      new NavigationView.OnNavigationItemSelectedListener() { 
       @Override 
       public boolean onNavigationItemSelected(MenuItem menuItem) { 
        selectDrawerItem(menuItem); 
        return true; 
       } 
      }); 
    } 
} 

ich nicht einen Fehler in meinem Code sehen ...

// Edit für bessere Informationen ...

Ok, ich habe es, aber jetzt einen neuen Fehler kommen ...

java.lang.N ullPointerException: Der Versuch, virtuelle Methode ‚android.view.Window $ Rückruf android.view.Window.getCallback()‘ auf ein Null-Objekt Referenz

aufzurufen, aber ich weiß nicht, wer Code ist es ...

Hier die XML

<?xml version="1.0" encoding="utf-8"?> 
 
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
 
    xmlns:tools="http://schemas.android.com/tools" 
 
    android:id="@+id/drawer_layout" 
 
    android:layout_width="match_parent" 
 
    android:layout_height="match_parent" 
 
    android:fitsSystemWindows="true" 
 
    tools:openDrawer="start"> 
 

 
    <include 
 
     layout="@layout/app_bar_navigation_drawer" 
 
     android:layout_width="match_parent" 
 
     android:layout_height="match_parent" /> 
 

 
    <android.support.design.widget.NavigationView 
 
     android:id="@+id/nav_view" 
 
     android:layout_width="wrap_content" 
 
     android:layout_height="match_parent" 
 
     android:layout_gravity="start" 
 
     android:fitsSystemWindows="true" 
 
     app:headerLayout="@layout/nav_header_navigation_drawer" 
 
     app:menu="@menu/activity_navigation_drawer_drawer" /> 
 

 
</android.support.v4.widget.DrawerLayout>

// edit ok ich machen es ... und den Fehler

Error: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.design.widget.NavigationView.setNavigationItemSelectedListener(android.support.design.widget.NavigationView$OnNavigationItemSelectedListener)' on a null object reference

kommen schon ...

public class NavigationDrawer extends AppCompatActivity { 
 
    private DrawerLayout NavDrawer; 
 
    private Toolbar toolbar; 
 
    private ActionBarDrawerToggle toggle; 
 
    
 
    @Override 
 
    protected void onCreate(Bundle savedInstanceState) { 
 
     super.onCreate(savedInstanceState); 
 
     setContentView(R.layout.activity_navigation_drawer); 
 

 
     toggle = new ActionBarDrawerToggle(this, NavDrawer, 
 
       toolbar, R.string.drawer_open, R.string.drawer_close) { 
 

 
      public void onDrawerClosed(View view) { 
 
       super.onDrawerClosed(view); 
 
       invalidateOptionsMenu(); 
 
      } 
 

 
      public void onDrawerOpened(View drawerView) { 
 
       super.onDrawerOpened(drawerView); 
 
       invalidateOptionsMenu(); 
 
      } 
 
     }; 
 

 
     toolbar = (Toolbar) findViewById(R.id.toolbar); 
 
     setSupportActionBar(toolbar); 
 

 
     NavDrawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
 

 
     NavigationView nvDrawer = (NavigationView) findViewById(R.id.nvView); 
 

 
     NavDrawer.setDrawerListener(toggle); 
 
    } 
 

 
    @Override 
 
    public boolean onOptionsItemSelected(MenuItem item) { 
 
    
 

 
     super.onOptionsItemSelected(item); 
 

 
     switch (item.getItemId()) { 
 
      case android.R.id.home: 
 
       NavDrawer.openDrawer(GravityCompat.START); 
 
       return true; 
 
     } 
 

 
     return super.onOptionsItemSelected(item); 
 
    } 
 

 
    @Override 
 
    protected void onPostCreate(Bundle savedInstanceState) { 
 
     super.onPostCreate(savedInstanceState); 
 
     toggle.syncState(); 
 
    } 
 

 
    @Override 
 
    public void onConfigurationChanged(Configuration newConfig) { 
 
     super.onConfigurationChanged(newConfig); 
 
     toggle.onConfigurationChanged(newConfig); 
 
    } 
 

 
    private void selectDrawerItem(MenuItem menuItem) { 
 
    
 
     Fragment fragment = null; 
 
     Class fragmentClass; 
 
     switch(menuItem.getItemId()) { 
 
      case R.id.nav_home: 
 
       fragmentClass = Home.class; 
 
       break; 
 
      case R.id.nav_artikel: 
 
       fragmentClass = Artikel.class; 
 
       break; 
 
      case R.id.nav_facebook: 
 
       fragmentClass = Facebook.class; 
 
       break; 
 
      case R.id.nav_instagram: 
 
       fragmentClass = Instagram.class; 
 
       break; 
 
      case R.id.nav_twitter: 
 
       fragmentClass = Twitter.class; 
 
       break; 
 
      case R.id.nav_manage: 
 
       fragmentClass = Settings.class; 
 
       break; 
 
      case R.id.nav_share: 
 
       fragmentClass = Share.class; 
 
       break; 
 
      case R.id.nav_send: 
 
       fragmentClass = Send.class; 
 
       break; 
 
      default: 
 
       fragmentClass = Home.class; 
 
     } 
 

 
     try { 
 
      fragment = (Fragment) fragmentClass.newInstance(); 
 
     } catch (Exception e) { 
 
      e.printStackTrace(); 
 
     } 
 

 
     FragmentManager fragmentManager = getSupportFragmentManager(); 
 
     fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit(); 
 
     menuItem.setChecked(true); 
 
     setTitle(menuItem.getTitle()); 
 
     NavDrawer.closeDrawers(); 
 
    } 
 

 
}

Dies ist Completted Navigation java ....

+1

Können Sie relevante XMLs posten? ZB "activity_navigation_drawer" ... –

+0

in der Antwort ... –

Antwort

0

Es ist wie Ihr Problem aussieht, ist, dass Sie nichts von Ihrem setupDrawerContent() zurückkehr . Also, wenn Sie ActionBarDrawerToggle toggle = setupDrawerToggle;toggle zuweisen wird ein Nullwert sein. Wenn Sie dann versuchen, addDrawerListener zu erhalten, erhalten Sie eine NullPointerException, um zu versuchen, einen Schubladen-Listener festzulegen, der null ist.

Unten finden Sie ein Beispiel von Google's Creating a Navigation Drawer für die ordnungsgemäße Einrichtung Ihrer Schublade Listener.

ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, mDrawerLayout, 
      R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close) { 

     /** Called when a drawer has settled in a completely closed state. */ 
     public void onDrawerClosed(View view) { 
      super.onDrawerClosed(view); 
      getActionBar().setTitle(mTitle); 
      invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() 
     } 

     /** Called when a drawer has settled in a completely open state. */ 
     public void onDrawerOpened(View drawerView) { 
      super.onDrawerOpened(drawerView); 
      getActionBar().setTitle(mDrawerTitle); 
      invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() 
     } 
    }; 
+0

Wo sehen Sie die 'addDrawerListener()' Methode in der Exception Nachricht? –

+0

Ich habe es in onCreate hinzufügen .. NavDrawer.addDrawerListener (toogle); –

Verwandte Themen