2

I need to start up my activity SearchResultsActivity when the user presses enter on the Search Widget. I already have my onSearchRequested function being called indirectly whenever I click on my menu_search which looks like this.

<item
    android:id="@+id/menu_search"
    android:title="@string/menu_search"
    appcompat:actionViewClass="android.support.v7.widget.SearchView"
    appcompat:showAsAction="always" />

Whenever I enter query and then enter/search nothing happens. I have set up my manifest file as below:

<!-- Main activity -->
    <activity
        android:name=".ui.activity.MainActivity"
        android:label="@string/app_name" >

        <meta-data
            android:name="android.default.searchable"
            android:value=".SearchResultsActivity"/>
    </activity>
    <!-- Search Results Activity -->
    <activity
        android:name=".ui.activity.SearchResultsActivity"
        android:label="@string/search_results_activity_title">
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>

        <meta-data
            android:name="android.app.searchable"
            android:resource="@xml/searchable" />
    </activity>

Main activity is where I perform the search and SearchResultsActivity receives my search query. I am not concerned about this at the moment. I just want a search to open up my SearchResultsActivity. I have tried a couple of tips like overriding my onOptionsItemSelected

case R.id.menu_search:
            Intent searchIntent = new Intent(MainActivity.this, SearchResultsActivity.class);;
            startActivity(searchIntent);
            break;

this still does not call my search activity. I have also associated searchable configuration with the SearchView on the onCreateOptionsMenu.

SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();

    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

    searchView.setIconifiedByDefault(false);

Adding this code returns a java.lang.ClassCastException

java.lang.ClassCastException: android.support.v7.widget.SearchView cannot be cast to android.widget.SearchView
        at com.nauv.jambomall.ui.activity.MainActivity.onCreateOptionsMenu(MainActivity.java:205)
        at android.app.Activity.onCreatePanelMenu(Activity.java:2578)
        at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:277)
        at android.support.v7.internal.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:84)
        at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallback.onCreatePanelMenu(AppCompatDelegateImplBase.java:251)
        at android.support.v7.app.AppCompatDelegateImplV7.preparePanel(AppCompatDelegateImplV7.java:1089)
        at android.support.v7.app.AppCompatDelegateImplV7.doInvalidatePanelMenu(AppCompatDelegateImplV7.java:1374)
        at android.support.v7.app.AppCompatDelegateImplV7.access$100(AppCompatDelegateImplV7.java:89)
        at android.support.v7.app.AppCompatDelegateImplV7$1.run(AppCompatDelegateImplV7.java:126)
        at android.os.Handler.handleCallback(Handler.java:730)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5414)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
        at dalvik.system.NativeStart.main(Native Method)

Does anyone have an idea on what I am doing wrong? Thanks.

Zack
  • 1,527
  • 2
  • 20
  • 32
  • you can find good example here [1]: http://stackoverflow.com/questions/11699206/cannot-get-searchview-in-actionbar-to-work/15735869#15735869 – Developer So far Jun 02 '15 at 06:43

2 Answers2

3

My guess is that you chose the wrong import, You should import

import android.support.v7.widget.SearchView;

instead of

import android.widget.SearchView;
RajaReddy PolamReddy
  • 22,428
  • 19
  • 115
  • 166
3

I have managed to fix this using this answer here. Previously by using

searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

I was assuming current activity is the searchable activity. All I had to do is add this code

ComponentName sa = new ComponentName(this, SearchResultsActivity.class);

which then set my searchable activity to SearchResultsActivity

Community
  • 1
  • 1
Zack
  • 1,527
  • 2
  • 20
  • 32