我尝试通过以下步骤在此处实现语音搜索;ActionBar MIC按钮(语音搜索)在搜索视图
中一切都很好,我已经将所有必要的信息添加到我的androidManifest和searcabhle.xml中,因此我能够看到麦克风图标和搜索栏,问题是当我尝试用声音搜索时,说明了什么,这不是't找到任何东西并崩溃,看起来setquery出了问题,这是完整的错误;
2019-03-13 04:55:55.723 1533-1533/com.demotxt.myapp.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.demotxt.myapp.myapplication, PID: 1533
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.SearchView.setQuery(java.lang.CharSequence, boolean)' on a null object reference
at com.demotxt.myapp.myapplication.activities.MainActivity.onNewIntent(MainActivity.java:153)
at android.app.Instrumentation.callActivityOnNewIntent(Instrumentation.java:1231)
at android.app.Instrumentation.callActivityOnNewIntent(Instrumentation.java:1243)
at android.app.ActivityThread.deliverNewIntents(ActivityThread.java:3064)
at android.app.ActivityThread.performNewIntents(ActivityThread.java:3076)
at android.app.ActivityThread.handleNewIntent(ActivityThread.java:3085)
at android.app.ActivityThread.-wrap17(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1710)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
这是我的主要3活动;
private static final String TAG = "Main3Activity";
private final String JSON_URL = "https://MYURLXXX" ;
private JsonArrayRequest request ;
private RequestQueue requestQueue ;
private List<Anime> lstAnime ;
private RecyclerView recyclerView ;
private RecyclerViewLiveAdapter adapter;
RecyclerView.LayoutManager layoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
lstAnime = new ArrayList<>() ;
recyclerView = findViewById(R.id.recyclerviewid);
jsonrequest();
}
private void jsonrequest() {
request = new JsonArrayRequest(JSON_URL, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
JSONObject jsonObject = null ;
for (int i = 0 ; i < response.length(); i++ ) {
try {
jsonObject = response.getJSONObject(i) ;
Anime anime = new Anime() ;
anime.setName(jsonObject.getString("name"));
anime.setDescription(jsonObject.getString("description"));
anime.setRating(jsonObject.getString("Rating"));
anime.setCategorie(jsonObject.getString("categorie"));
anime.setStudio(jsonObject.getString("studio"));
anime.setImage_url(jsonObject.getString("img"));
anime.setLink(jsonObject.getString("link"));
anime.setDrm_scheme(jsonObject.getString("drm_scheme"));
anime.setDrm_license_url(jsonObject.getString("drm_license_url"));
anime.setDrm(jsonObject.getString("drm"));
anime.setSubtitle(jsonObject.getString("subtitle"));
anime.setSubtitle1(jsonObject.getString("subtitle1"));
anime.setSubtitle2(jsonObject.getString("subtitle2"));
lstAnime.add(anime);
} catch (JSONException e) {
e.printStackTrace();
}
setuprecyclerview(lstAnime);
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue = Volley.newRequestQueue(Main3Activity.this);
requestQueue.add(request) ;
}
private void setuprecyclerview(List<Anime> lstAnime) {
adapter = new RecyclerViewLiveAdapter(this,lstAnime) ;
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
}
@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 = (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;
}
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
searchView.setQuery(query, false);
}
}
}
我确实在适配器中过滤一些东西,但是由于在这里发生错误,我认为无需将我的适配器代码放在此处,而且由于文本过滤完美工作,唯一的问题是语音搜索。
那是因为您正在访问onNewIntent()中的null搜索视图对象。为您的课程中的searchView
创建一个全局参考变量,并在OnCreateOptionsMenu()中实例化。如下:
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;
}