我正在尝试将简单的地图应用与地点搜索功能集成,我面临的问题是搜索栏没有在地图中出现。我不确定是什么原因,因为我是android的初学者。但onCreateOptionsMenu
代码如下,请任何解决方案都非常感谢。
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
// get a reference to the EditText widget for the search option
View searchRef = menu.findItem(R.id.action_search).getActionView();
mSearchEditText = (EditText) searchRef.findViewById(R.id.searchText);
// set key listener to start search if Enter key is pressed
mSearchEditText.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {
if (keyCode == KeyEvent.KEYCODE_ENTER) {
onSearchButtonClicked(mSearchEditText);
return true;
}
return false;
}
});
return true;
}
错误:
E / AndroidRuntime:致命异常:主要 java.lang.NullPointerException at com.example.adrian.placesearch.MainActivity.onCreateOptionsMenu(MainActivity.java:106)