菜单中的微调器始终为null

时间:2017-05-11 15:23:03

标签: android android-layout nullpointerexception

所以我按照这个链接将微调器添加到导航抽屉: How to add Spinner as an item in Navigation Drawer

这是我的代码:

@BindView(R.id.drawerLayout)
DrawerLayout mDrawerLayout;
@BindView(R.id.toolbar)
Toolbar toolbar;
@BindView(R.id.navigation_view)
NavigationView navigationView;
@BindView(R.id.navigation_filter_view)
NavigationView navigationFilterView;
Spinner citiesSpinner;
private ActionBarDrawerToggle mToggle;

private View navigationHeaderView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Iconify.with(new FontAwesomeModule());
    setContentView(R.layout.activity_main);
    ButterKnife.bind(this);
    initNavigationDrawer();
    toolbar.setTitle("");
    setSupportActionBar(toolbar);
    navigationHeaderView = LayoutInflater.from(this).inflate(R.layout.navigation_header, null);
    citiesSpinner = (Spinner) navigationFilterView.getMenu().findItem(R.id.city_spinner).getActionView();
    citiesSpinner.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, getResources().getStringArray(R.array.cities)));

}

city_spinner布局:

<Spinner xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/citiesSpinner"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:gravity="center_vertical">
</Spinner>

navigationFilterView:

<android.support.design.widget.NavigationView
    android:id="@+id/navigation_filter_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="end"
    android:background="@color/colorAccent"
    app:menu="@menu/search_menu">
 </android.support.design.widget.NavigationView>

search_menu:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:id="@+id/city_spinner"
    android:actionLayout="@layout/city_spinner"
    android:title="" />
</menu>

问题是citiesSpinner总是为空

  

尝试在空对象引用上调用虚方法'void android.widget.Spinner.setAdapter(android.widget.SpinnerAdapter)'

0 个答案:

没有答案