为什么我在 Android Studio 的 SearchView 和 OnQueryTextListener 上收到此



我一直在尝试在这个应用程序中构建一个可搜索的界面一段时间,我一直在使用很多Android开发人员指南来提供帮助,但是我最近遇到了这个错误与SearchView和OnQueryTextListener有关,我不知道如何解决,但它阻止了我的应用程序运行。

这是我的主要活动:

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
        setSupportActionBar(myToolbar);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.overflow, menu);   
        MenuItem searchItem = menu.findItem(R.id.search);
        SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
        searchView.setOnQueryTextListener((OnQueryTextListener) searchView);
//this is where the program crashes
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);

        SearchableInfo info = searchManager.getSearchableInfo(getComponentName());
        searchView.setSearchableInfo(info);
        return true;
    }

    public boolean onQueryTextSubmit(String query) {
        // User pressed the search button
        return false;
    }

    public boolean onQueryTextChange(String newText) {
        // User changed the text
        return false;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.constants:
                startActivity(new Intent(MainActivity.this, FundamentalPhysicalConstants.class));
                return true;
            case R.id.joes_rules:
                //go to rules
                return true;
            case R.id.home:
                //Go back to the home screen
                return true;
            case R.id.search:
                //open search
                return true;
            default:
                // If we got here, the user's action was not recognized.
                // Invoke the superclass to handle it.
                return super.onOptionsItemSelected(item);
        }
    }
}

这是我的可搜索活动:

public class SearchableActivity extends ListActivity {
    DatabaseTable db= new DatabaseTable(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_searchable);
//        get the intent sent when user searches from search widget, verify the action and exxtract what is typed in
        Intent intent = getIntent();
        handleIntent(intent);
        }

    public void onNewIntent(Intent intent) {
        setIntent(intent);
        handleIntent(intent);
    }
    private void handleIntent(Intent intent) {
        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            String query = intent.getStringExtra(SearchManager.QUERY);
            Cursor c = db.getWordMatches(query, null);
            //still need to process Cursor and display results
        }
    }
    public void onListItemClick(ListView l,
                                View v, int position, long id) {
        // call detail activity for clicked entry
    }

    private void doSearch(String queryStr) {
        // get a Cursor, prepare the ListAdapter
        // and set it
    }   
}

这是我可搜索的.xml:

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="@string/app_name"
    android:hint="@string/search_hint">
</searchable>

这是我的日志:

06-08 14:57:09.581 28388-28388/com.gmd.referenceapplication E/AndroidRuntime: FATAL EXCEPTION: main
                                                                              Process: com.gmd.referenceapplication, PID: 28388
                                                                              java.lang.ClassCastException: android.support.v7.widget.SearchView cannot be cast to android.support.v7.widget.SearchView$OnQueryTextListener
                                                                                  at com.gmd.referenceapplication.MainActivity.onCreateOptionsMenu(MainActivity.java:55)
                                                                                  at android.app.Activity.onCreatePanelMenu(Activity.java:2846)
                                                                                  at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:340)
                                                                                  at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:85)
                                                                                  at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:258)
                                                                                  at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:85)
                                                                                  at android.support.v7.app.ToolbarActionBar.populateOptionsMenu(ToolbarActionBar.java:454)
                                                                                  at android.support.v7.app.ToolbarActionBar$1.run(ToolbarActionBar.java:61)
                                                                                  at android.os.Handler.handleCallback(Handler.java:739)
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                  at android.os.Looper.loop(Looper.java:148)
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

我试图对此做很多研究,但我没有任何运气。 我将非常感谢有关此事的任何帮助,谢谢。

这一行是错误的:

searchView.setOnQueryTextListener((OnQueryTextListener) searchView);

不能searchView转换为OnQueryTextListener。而是给它一个适当的参数作为匿名类,如下所示:

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            //your logic
        }
        @Override
        public boolean onQueryTextChange(String newText) {
            //your logic
        }
    });

我现在看到您尝试在活动中添加这两种方法,这不是正确的方法。您需要创建实现此SearchView.OnQueryTextListener接口的自定义类,然后说:

searchView.setOnQueryTextListener(new MyClassThatImplementsTheInterface);

或者按照上面我给你的第一个解决方案的方式去做。这是使用匿名类的更常见的方法。

日志中可以清楚地看出错误,您将SearchView投射到错误的侦听器尝试类似

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
    @Override
    public boolean onQueryTextSubmit(String query) {
        return false;
        }
        @Override
        public boolean onQueryTextChange(String newText) {
            return false;
        }
    });`

相关内容

  • 没有找到相关文章

最新更新