setDrawerListener 崩溃 android 应用程序
setDrawerListener crashes android app
我正在开发 android 应用程序,其中,我希望单个 activity 上的 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: 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
这是布局 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>
您的布局中没有名称为 layout_dashboard
的 android:id="@+id/drawer_layout"
视图。
UPD
您这边似乎对如何使用有误解DrawerLayout
。 There is good article关于那个,请检查并让我知道是否还有任何问题。
请将您的 xml 文件更改为以下内容,我已将 DrawerLayout
和 NavigationView
添加到您的布局中,但缺少您的布局文件。
<?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>
我正在开发 android 应用程序,其中,我希望单个 activity 上的 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: 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
这是布局 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>
您的布局中没有名称为 layout_dashboard
的 android:id="@+id/drawer_layout"
视图。
UPD
您这边似乎对如何使用有误解DrawerLayout
。 There is good article关于那个,请检查并让我知道是否还有任何问题。
请将您的 xml 文件更改为以下内容,我已将 DrawerLayout
和 NavigationView
添加到您的布局中,但缺少您的布局文件。
<?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>