1

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.

curious_coder
  • 2,392
  • 4
  • 25
  • 44
  • 1
    what is on Line 64 `Exclude_list.java`? – Raghunandan May 04 '14 at 16:59
  • `searchView` must be null. Are you sure it is in the `activity_exclude_list` layout and has the id: `options_menu_main_search`? – Tamby Kojak May 04 '14 at 17:01
  • Do you use `ActionBarActivity` as activity extension? – Blo May 04 '14 at 17:03
  • @Fllo: no.Just `public class Exclude_list extends Activity{}` – curious_coder May 04 '14 at 17:04
  • searchview is a menu item that appears in the action bar. But you initialize it in Activity? You are using SearchView from the supporot library?? – Raghunandan May 04 '14 at 17:04
  • Put a breakpoint on that line and inspect each element. Which one is null? – Simon May 04 '14 at 17:06
  • @tambykojak: Should it be present be activity_exclude_list layout? Currently ther's one TextView and one ListView in RelativeLayout. – curious_coder May 04 '14 at 17:07
  • @Raghunandan: I even tried using `android:actionViewClass="android.widget.SearchView"` but still same error. – curious_coder May 04 '14 at 17:08
  • If i remove those line, the search is working but I am unable to fetch any query when typed in search. – curious_coder May 04 '14 at 17:09
  • 1
    @curious_coder that change does not make any difference as you have a NPE. You need to initialize the search view in `onCreateOptionsMenu`. Read the same @ http://developer.android.com/guide/topics/ui/actionbar.html – Raghunandan May 04 '14 at 17:15
  • @Raghunandan: Thanks.. Unfortunately I removed the code from `onCreateOptionsMenu` because of another bug and after resolving the bug, I paste it in wrong onCreate. – curious_coder May 04 '14 at 17:18

1 Answers1

1

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"
Blo
  • 11,903
  • 5
  • 45
  • 99