1

First of all, thank you all for your (future) responses :)

Below explanation of my problem:

I have my main Activity named MainActivity and I use a Fragment inside it. So, my Fragment contains a SearchView and a ListView which will contains suggestions depending on user's input... Please notice that I don't use it in the ActionBar but in my layout file...

Here is fragment_board_menu.xml, xml file lied to my Fragment BoardMenuFragment :

<android.support.v7.widget.SearchView
        android:id="@+id/board_searchView"
        android:layout_marginLeft="3dp"
        android:layout_marginStart="3dp"
        android:background="@color/green"
        app:searchIcon="@drawable/ic_search"
        app:searchHintIcon="@drawable/ic_search"
        app:closeIcon="@drawable/ic_close"
        app:defaultQueryHint="@string/create_newboard_hint"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <ListView
        android:layout_below="@+id/board_searchView"
        android:background="@drawable/listview_border"
        android:layout_marginBottom="@dimen/listview_border_marginBottom"
        android:layout_marginLeft="3dp"
        android:layout_marginStart="3dp"
        android:id="@+id/board_listView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

He is BoardMenuFragment.java, my Fragment java file :

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_board_menu, container, false);
    setupSearchView(view);

    // LISTVIEW
    // TODO This view is an example, don't forget to delete it
    values = new ArrayList<>();
    values.add("pomme");
    values.add("poire");
    values.add("banane");
    values.add("cerise");
    values.add("citron");
    adapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_expandable_list_item_1, android.R.id.text1, values);
    board_listView.setAdapter(adapter);

    return view;
}

private void setupSearchView(View view) {
    SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
    final SearchView searchView = (SearchView) view.findViewById(R.id.searchView);
    SearchableInfo searchableInfo = searchManager.getSearchableInfo(getActivity().getComponentName());
    searchView.setSearchableInfo(searchableInfo);
}

But I have an exception which is :

 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.SearchView.setSearchableInfo(android.app.SearchableInfo)' on a null object reference

So my question is : is it possible to use SearchView in Fragment using SearchManager and SearchableInfo because I tried to do it using setOnQueryTextListener and it works but I don't think that is the best way to do it because I saw a lot of tutorials where you create a SearchableActivity...

d3vpasha
  • 157
  • 1
  • 17

1 Answers1

0

I had a similar question and found the answer in this question You need to get the component name for the SearchableActivity.

new ComponentName(this, SearchableActivity.class)
Community
  • 1
  • 1
Patrick Lacz
  • 181
  • 1
  • 4