语音障碍后SearchView抛出NullPointerException

时间:2019-03-13 02:03:10

标签: android nullpointerexception searchview

我尝试通过此处的以下步骤实施语音搜索; ActionBar Mic Button(Voice Search) in SearchView

一切都很好,我已经将所有必要的信息添加到我的AndroidManifest和searcabhle.xml中,因此我能够看到麦克风图标和搜索栏,问题是当我说完某句话后尝试用语音搜索时,它没有什么都找不到,应用程序崩溃,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)

这是我的Main3Activity;

 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);
        }
    }
}

我确实过滤适配器中的内容,但是由于这里发生错误,因此我认为无需在此处放置适配器代码,而且由于文本过滤效果很好,唯一的问题是语音搜索。

5 个答案:

答案 0 :(得分:0)

我现在尝试的是这部分内容;

@Override
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);
    }
}

我拿了searchView.setQuery(query, false); 并替换为adapter.getFilter().filter(query);}

现在它可以搜索了,但没有在搜索栏中显示文本,也没有后退按钮。我也认为这是一个很大的错误,因为我尝试通过手动设置setQuery仍然没有用,这是怎么回事可能吗?

简而言之,当我单击麦克风按钮时,它会捕获我的单词并将其放入搜索栏。

答案 1 :(得分:0)

我尝试过的最后一件事是;

删除了onNewIntent方法并将此代码添加到onCreateOptionsMenu中,没有错误,但再次没有执行语音搜索,甚至没有在搜索栏中放置任何内容

Intent intent = getIntent();
    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        String query = intent.getStringExtra(SearchManager.QUERY);
        searchView.setQuery(query,false);
    }

我真的需要帮助。

答案 2 :(得分:0)

最后,返回第一个代码,并以很少的更改替换了onNewIntent方法,仍然保持不变

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        String query = intent.getStringExtra(SearchManager.QUERY);
            searchView.onActionViewExpanded();
            searchView.setQuery(query,false);
    }
}

答案 3 :(得分:0)

很抱歉为您添加帮助的答案,但这确实使我无法自拔,确实需要帮助:/

这是我为延迟而添加的最新代码,但仍然没有幸免,所以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);
            }
        });

答案 4 :(得分:0)

那是因为您正在onNewIntent()中访问一个空的searchView对象。在您的课程中为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;
}