我有一个工具栏,可以为我的Material Searchview图标返回nullpointer异常。
这是我返回searchview项目的方法
MaterialSearchView searchView = (MaterialSearchView) findViewById(R.id.search_view);
searchView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Do some magic
String clubName = (String) parent.getItemAtPosition(position);
//double latitude = Double.parseDouble(item.);
Club selectedClub = getClub(clubName);
PlotMarker(selectedClub.getLat(), selectedClub.getLng());
address.setText(selectedClub.getAddress());
description.setText(selectedClub.getDesc());
name.setText(selectedClub.getName1());
Log.i("TAG", "http://partygoer.org"+selectedClub.getImage());
Picasso.with(MapActivity.this).load("http://partygoer.org"+selectedClub.getImage()).into(imageClub);
}
});
下面是我选择菜单项
的地方@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem item = menu.findItem(R.id.action_search);
**searchView.setMenuItem(item);**
return true;
}
这是我的xml,setMenuItem返回空指针异常
<FrameLayout
android:id="@+id/toolbar_container"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorAccent" />
<com.miguelcatalan.materialsearchview.MaterialSearchView
android:id="@+id/search_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</FrameLayout>
这是错误日志
01-20 07:54:05.564 14209-14209/com.lawerh.jonathan.partygoer E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.lawerh.jonathan.partygoer, PID: 14209
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.miguelcatalan.materialsearchview.MaterialSearchView.setMenuItem(android.view.MenuItem)' on a null object reference
at com.lawerh.jonathan.partygoer.ui.MapActivity.onCreateOptionsMenu(MapActivity.java:122)
at android.app.Activity.onCreatePanelMenu(Activity.java:2889)
at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:362)
at android.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:76)
at com.android.internal.app.ToolbarActionBar.populateOptionsMenu(ToolbarActionBar.java:447)
at com.android.internal.app.ToolbarActionBar$1.run(ToolbarActionBar.java:55)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5728)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)