0

I'm trying to implement a search functionality in my actionbar, but it is not working as expected, when I run the app I get the following error:

 java.lang.NullPointerException
            at br.com.representemais.FragmentClientes.onCreateOptionsMenu(FragmentClientes.java:80)

Line 80: searchView.setQueryHint(this.getString(R.string.search));

@Override
    public void onCreate(Bundle savedInstanceState) {
        shouldExecuteOnResume = false;

        super.onCreate(savedInstanceState);
        // TODO Auto-generated method stub
        setContentView(R.layout.fragment_cliente);




    }



@Override
public boolean onCreateOptionsMenu(Menu menu ) {
    // TODO Auto-generated method stub
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);


    searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(Menus.PROCURAR));
    searchView.setQueryHint(this.getString(R.string.search));

    ((EditText)searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text))
    .setHintTextColor(getResources().getColor(R.color.white));
    searchView.setOnQueryTextListener(OnQuerySearchView);



    menu.findItem(Menus.PROCURAR).setVisible(true);



    return super.onCreateOptionsMenu(menu);

}

XML:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto" >

    <item
        android:id="@+id/menu_search"
        android:icon="@drawable/ic_action_search"
        android:title="@string/search"
        app:actionViewClass="android.support.v7.widget.SearchView"
        app:showAsAction="ifRoom|collapseActionView"
        />





</menu>
John
  • 107
  • 1
  • 3
  • 15

2 Answers2

0

Which line is giving you the error exactly ?

Try doing this if your API level is 11 or higher

SearchView search = (SearchView) menu.findItem(R.id.menu_search).getActionView();

Also i think the editText which you are setting the hintTextColor is actually a textview

TextView searchText = (TextView)searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);

Refer to this question : SearchView edittext is always null

Community
  • 1
  • 1
Android2390
  • 1,150
  • 12
  • 21
0

Just found what my problem was, I had to extend my class to ActionBarActivity, now it`s working, thank you all.

John
  • 107
  • 1
  • 3
  • 15