Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a null SearchView

I know this already has been asked many times but I've read all of them and tried changing lot's of thing and none of them has worked.
I'm writing an app which supports only android 4 and more so I don't need support library v7 (formerly it was included though). My code is very straight forward:

public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();

    searchView.setOnQueryTextListener(new SearchViewQueryTextListener());
    return super.onCreateOptionsMenu(menu);
}

I'm getting NullPointerException on 4th line. Here is a link to my repo so you can see anything you want. I think myself, that problems lies in MainActivity class or menu.xml or in application Manifest.

Please help, I'm stucked.

like image 626
Alireza Farahani Avatar asked Dec 05 '25 20:12

Alireza Farahani


1 Answers

You have a small typo in your main.xml menu file resource. You wrote android.widget.searchview, this should be android.widget.SearchView(case matters as there isn't any searchview widget in the android SDK explaining the missing(null) widget).

like image 106
user Avatar answered Dec 08 '25 11:12

user



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!