android studio-setquery在第一次搜索时不工作

to94eoyn  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(221)

我正在开发一个基于中文字典的android应用程序。我正在使用handleint从其他活动进行原始搜索。计划是打印这些结果,并将查询保留在搜索栏中。
我的问题是,当更改为results活动时,查询将消失,但在第一次搜索之后的每一次搜索都会在搜索栏中显示查询(由此我假设onnewintent正在工作,但handleintent不工作)。它正在进行搜索,我只希望查询留在搜索栏中。
我使用了调试器,通过打印变量来确保查询在函数中执行,并且它执行任务时没有问题,但是在尝试使用setquery之后,整个过程在模拟器上崩溃了。
日志显示:
原因:java.lang.nullpointerexception:尝试对celadondragon.com.activities上的空对象引用调用虚拟方法“void android.support.v7.widget.searchview.setquery(java.lang.charsequence,boolean)”

//Handles the original search from a different activity
@Override
protected void handleIntent(Intent intent) {
    //boolean mybool = Intent.ACTION_SEARCH.equals(intent.getAction());
    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        final String query = intent.getStringExtra(SearchManager.QUERY);
        searchDictionary(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);
            }
        });
    }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题