curious_coder
curious_coder

Reputation: 2458

Search in Android not working

I have been struggling with search feature in android for days. Unable to pinpoint the bug.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_exclude_list);

    .......

    SearchView searchView = (SearchView)findViewById(R.id.options_menu_main_search);
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    if(null!=searchManager ) {
      searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    }

    handleIntent(getIntent());

}

@Override
public void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    setIntent(intent);
    handleIntent(intent);
}

private void handleIntent(Intent intent) {
    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        String query = intent.getStringExtra(SearchManager.QUERY);
        Log.i("q",query);
    }
}

searchable.xml

<searchable xmlns:android="http://schemas.android.com/apk/res/android"
 android:hint="@string/search_hint"
 android:includeInGlobalSearch="false"
 android:label="@string/app_name"
 android:searchSettingsDescription="@string/search_hint" />

Menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <!-- Search, should appear as action button -->
    <item
        android:id="@+id/options_menu_main_search"
        android:actionViewClass="android.support.v7.widget.SearchView"
        android:icon="@drawable/ic_action_search"
        android:showAsAction="always"
        android:title="Search"/>
</menu>

Manifest

<activity
 android:name="com.xyz.Exclude_list"
 android:label="@string/title_activity_exclude_list"
 android:screenOrientation="portrait"
 android:launchMode="singleTop">
 <intent-filter>
    <action android:name="android.intent.action.SEARCH" />
    <action android:name="android.intent.action.VIEW" />
 </intent-filter>

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

Error

Caused by: java.lang.NullPointerException
  at com.xyz.Exclude_list.onCreate(Exclude_list.java:64)

Line 64: searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

If i remove

 SearchView searchView = (SearchView)findViewById(R.id.options_menu_main_search);
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    if(null!=searchManager ) {
      searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    }

the search is working but I am unable to fetch/Log any query when I give input in search.

Upvotes: 1

Views: 849

Answers (1)

Blo
Blo

Reputation: 11978

You are trying to initialize a MenuItem inside onCreate method. You should initialize it inside onCreateOptionsMenu method as:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu, menu);
    SearchView searchView = (SearchView) menu.findItem(R.id.options_menu_main_search).getActionView();
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    if(null!=searchManager ) {
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    }
}  

See the Using Search Widget topic from Documentation, specially Configuring the search widget section. Also, because you're not using the AppCompat library, you should use this class (as declared in the docs):

android:actionViewClass="android.widget.SearchView"

Upvotes: 1

Related Questions