setDrawerListener崩溃Android应用程序

时间:2016-07-20 09:57:04

标签: java android navigation-drawer android-spinner android-toolbar

我正在开发Android应用程序,其中,我希望Menudrawer和Spinner进行单项活动。

代码段:

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);
}

我已经编写了上面的代码,但它崩溃了以下错误:

  

Java.lang.NullPointerException:尝试调用虚方法'void   android.support.v4.widget.DrawerLayout.setDrawerListener(android.support.v4.widget.DrawerLayout $ DrawerListener)”   在null对象引用   android.app.ActivityThread.performLaunchActivity

这是布局xml文件:

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>

2 个答案:

答案 0 :(得分:1)

您的布局中没有名称为android:id="@+id/drawer_layout"的{​​{1}}视图。

UPD

似乎您的误解是如何使用layout_dashboardThere is good article关于这一点,如果还有任何问题,请检查并告诉我。

答案 1 :(得分:0)

请将您的xml文件更改为以下内容,我已将DrawerLayoutNavigationView添加到缺少布局文件的布局中。

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