简体   繁体   English

为什么在Android Studio中的SearchView和OnQueryTextListener上出现此错误?

[英]Why am I getting this error in Android Studio on SearchView and OnQueryTextListener?

I have been trying to build a searchable interface in this app for a while, and I've been using a lot of the Android developer guides to help, but I recently ran into this error to do with SearchView and OnQueryTextListener that I do not know how to resolve, but it is stopping my app from running. 我一段时间以来一直在尝试在此应用程序中构建可搜索的界面,并且一直在使用许多Android开发人员指南来提供帮助,但是最近我遇到了与我不知道的SearchView和OnQueryTextListener有关的错误如何解决,但这正在阻止我的应用运行。

Here is my MainActivity: 这是我的MainActivity:

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

        }
    }
}

Here is my SearchableActivity: 这是我的SearchableActivity:

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
    }   

}

Here is my searchable.xml: 这是我的searchable.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>

Here is my logcat: 这是我的日志:

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)

I have tried to do a lot of research on this, and I haven't had any luck. 我已经尝试对此进行了大量研究,但我没有任何运气。 I would be extremely grateful for any help on this matter, thanks. 非常感谢您在此问题上的帮助。

This line is just wrong: 这行是错误的:

searchView.setOnQueryTextListener((OnQueryTextListener) searchView);

You cannot cast the searchView into OnQueryTextListener . 不能searchView OnQueryTextListener转换为OnQueryTextListener Instead give it a proper parameter as an anonymous class like this: 而是给它一个适当的参数作为这样的匿名类:

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

        @Override
        public boolean onQueryTextChange(String newText) {
            //your logic
        }
    });

I now see that you have tried to add these two methods in your Activity, which is not the proper way to do it. 现在,我看到您已经尝试在Activity中添加这两种方法,但这不是正确的方法。 You either need to create your custom class that implements this SearchView.OnQueryTextListener interface, and then say: 您要么需要创建实现此SearchView.OnQueryTextListener接口的自定义类,然后说:

searchView.setOnQueryTextListener(new MyClassThatImplementsTheInterface);

Or do it the way above that I gave you as the first solution. 或者按照我作为第一个解决方案的方式进行操作。 That is the more common way of doing this, using anonymous classes. 这是使用匿名类执行此操作的更常见方法

the error is clear from the logs you are casting the SearchView to a listener which is wrong try something like 错误是从您将SearchView投射到侦听器的日志中清除的,尝试类似

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

        @Override
        public boolean onQueryTextChange(String newText) {
            return false;
        }
    });`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM