-1

Ich entwickle Android-App, in denen, will ich Menudrawer und Spinner auf einzelne Aktivität.setDrawerListener stürzt Android-App

Code-Snippet:

public class Dashboard extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener { 

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.layout_dashboard); 


    // Spinner element 
     Spinner spinner1 = (Spinner) findViewById(R.id.spinner); 

    // Creating adapter for spinner 
     ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories); 

    // Drop down layout style - list view with radio button 
     dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 

    // attaching data adapter to spinner 
    spinner1.setAdapter(dataAdapter); 

    // Spinner click listener 
    spinner1.setOnItemSelectedListener(new OnItemSelectedListener() { 
     @Override 
     public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) { 


      // Showing selected spinner item 
       Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show(); 

     } 

     @Override 
     public void onNothingSelected(AdapterView<?> parentView) { 
      // your code here 
     } 

    }); 


    //Adding MenuDrawer... 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
       this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
     drawer.setDrawerListener(toggle); 
     toggle.syncState(); 


     NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
     navigationView.setNavigationItemSelectedListener((NavigationView.OnNavigationItemSelectedListener) this); 
} 

Ich habe den obigen Code geschrieben, aber es stürzt mit dem folgenden Fehler:

Java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.widget.DrawerLayout.setDrawerListener(android.support.v4.widget.DrawerLayout$DrawerListener)' on a null object reference at android.app.ActivityThread.performLaunchActivity

Hier ist das Layout XML-Datei:

layout_dashboard.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    > 


    <Spinner 
     android:layout_width="match_parent" 
     android:layout_height="50dp" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="10dp" 
     android:layout_marginTop="10dp" 
     android:id="@+id/spinner" 
     android:background="@drawable/spinner_border"/> 

    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:inputType="phone" 
     android:ems="10" 
     android:id="@+id/quantityField" 
     android:hint="Qunatity" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="10dp" 
     android:layout_marginTop="10dp" /> 

    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:inputType="phone" 
     android:ems="10" 
     android:id="@+id/priceField" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="10dp" 
     android:layout_marginTop="10dp" 
     android:hint="Price" /> 

    <Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="5 August 2016" 
     android:id="@+id/dateBtn" 
     android:height="60dp" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="10dp" 
     android:layout_marginTop="10dp" /> 


    <Button 
     android:layout_width="200dp" 
     android:layout_height="wrap_content" 
     android:text="Add" 
     android:id="@+id/addBtn" 
     android:layout_margin="10dp" 
     android:layout_gravity="center_horizontal" 
     android:background="@drawable/button_shape" 
     android:textColor="#ffffff" 
     /> 


</LinearLayout> 
+0

Bitte teilen Sie Ihre Logcat – SaravInfern

+0

Zeigen Sie Ihre layout_dashboard.xml-Datei. – Drv

+0

http://imgur.com/a/PsjZ6 – Krunal

Antwort

0

Bitte ändern Sie Ihre XML-Datei nach unten, ich habe hinzugefügt DrawerLayout und NavigationView zu Ihrem Layout, die Ihre Layout-Datei fehlte.

<?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"> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
> 


<Spinner 
    android:layout_width="match_parent" 
    android:layout_height="50dp" 
    android:layout_marginLeft="10dp" 
    android:layout_marginRight="10dp" 
    android:layout_marginTop="10dp" 
    android:id="@+id/spinner" 
    android:background="@drawable/spinner_border"/> 

<EditText 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:inputType="phone" 
    android:ems="10" 
    android:id="@+id/quantityField" 
    android:hint="Qunatity" 
    android:layout_marginLeft="10dp" 
    android:layout_marginRight="10dp" 
    android:layout_marginTop="10dp" /> 

<EditText 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:inputType="phone" 
    android:ems="10" 
    android:id="@+id/priceField" 
    android:layout_marginLeft="10dp" 
    android:layout_marginRight="10dp" 
    android:layout_marginTop="10dp" 
    android:hint="Price" /> 

<Button 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="5 August 2016" 
    android:id="@+id/dateBtn" 
    android:height="60dp" 
    android:layout_marginLeft="10dp" 
    android:layout_marginRight="10dp" 
    android:layout_marginTop="10dp" /> 


<Button 
    android:layout_width="200dp" 
    android:layout_height="wrap_content" 
    android:text="Add" 
    android:id="@+id/addBtn" 
    android:layout_margin="10dp" 
    android:layout_gravity="center_horizontal" 
    android:background="@drawable/button_shape" 
    android:textColor="#ffffff" 
    /> 


</LinearLayout> 


<android.support.design.widget.NavigationView 
    android:id="@+id/nav_view" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:layout_gravity="start" 
    app:itemTextAppearance="?attr/textAppearanceListItem" 
    app:menu="@menu/drawer" /> 
</android.support.v4.widget.DrawerLayout> 
+0

Fehler hier angezeigt: 'Fehler beim Parsen von XML: Junk nach Dokumentelement' – Krunal

+0

können Sie die vollständige Fehlermeldung posten? – himanshu1496

+0

und nur um klar zu sein, übergeben Sie die Menü-Datei 'drawer.xml', die Menüoptionen in' navigationView' enthält? – himanshu1496

1

Es gibt keine Ansicht mit android:id="@+id/drawer_layout" in Ihrem Layout mit dem Namen layout_dashboard.

UPD

scheint von Ihrer Seite ist Missverständnis, wie DrawerLayout zu verwenden. There is good article darüber, bitte überprüfen Sie und lassen Sie mich wissen, wenn es noch Fragen gibt.

+0

'layout_dashboard' ist meine XML-Datei und ist im Layout-Ordner vorhanden. – Krunal

+1

@Krunal Ja, aber es gibt keine Ansicht mit id = drawer_layout. Post your drawer_layout – Divers

+0

@Krunal Ich habe meine Antwort aktualisiert. Bitte prüfe. – Divers