我尝试通过以下步骤实现语音搜索; SearchView中的ActionBar Mic按钮(语音搜索)
一切都很好,我已经将所有必要的信息添加到我的AndroidManifest和searcabhle.xml ……
那是因为您正在访问onNewIntent()中的null searchView对象。为。创建全局引用变量 searchView 在你的类中并在onCreateOptionsMenu()中实例化。如下所示:
searchView
SearchView searchView; @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.menu_item,menu); MenuItem searchItem = menu.findItem(R.id.action_search); searchView = (SearchView) searchItem.getActionView(); searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { @Override public boolean onQueryTextSubmit(String query) { return false; } @Override public boolean onQueryTextChange(String newText) { if (adapter != null){ adapter.getFilter().filter(newText);} return false; } }); return true; }
伙计们抱歉找到帮助的答案,但这让我活着,需要亲自动手:/ /
这是我为延迟目的添加的最新代码,但没有运气再次仍为null searchView;
if (Intent.ACTION_SEARCH.equals(intent.getAction())) { final String query = intent.getStringExtra(SearchManager.QUERY); searchView.post(new Runnable() { @Override public void run() { // Important! Make sure searchView has been initialized // and referenced to the correct (current) SearchView. // Config changes (e.g. screen rotation) may make the // variable value null. searchView.setQuery(query, false); } });