3

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.

Alireza Farahani
  • 2,238
  • 3
  • 30
  • 54
  • with only that to go on, we won't be able to do much, I guess. why not checking for searchView == null before calling a method on it? – Stultuske May 15 '14 at 08:57
  • I've put a link to entire my app! And as I said searchView is null in 6th line. – Alireza Farahani May 15 '14 at 09:06
  • if it is null, don't call a method on it. – Stultuske May 15 '14 at 09:12
  • You say you don't need the support library, then why are you using it? You're using `android.support.v7.widget.SearchView` in your xml and `MenuItemCompat` in your code so you're clearly using the support library. – darnmason May 15 '14 at 09:53
  • @darnmason: I've corrected that, still gets the same error – Alireza Farahani May 15 '14 at 09:56
  • Now your repo and question are out of sync, all I can say is make sure you're importing the correct classes and either stick with the support versions or remove all references to the support library. – darnmason May 15 '14 at 09:56
  • @darnmason: Thank you for remembering me I've updated the repo. – Alireza Farahani May 15 '14 at 10:02
  • Probably a typo but I've seen that in your `main.xml` file you wrote `android:actionViewClass="android.widget.searchview"/>`. It should be `android.widget.SearchView` instead of `android.widget.searchview`. – user May 19 '14 at 09:24
  • @Luksprog Yes, thanks! It was as you said. Write an answer and I mark it as answer ;) – Alireza Farahani May 21 '14 at 08:01

2 Answers2

1

The problem with the code is SearchView is not a MenuItem so cannot be obtained by menu.findItem. You have to get the menu item for search first and then get your SearchView.

Replace the failing line with -

mSearchMenuItem = menu.findItem(R.id.action_search);
mSearchView = (EnglishVerbSearchView) MenuItemCompat.
               getActionView(mSearchMenuItem);

WHERE,

R.id.action_search is the id of your search item in the menu.

Also since you are using the compat library so your menu xml file should contain the yourapp namespace.

For example --

<menu xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
    <item android:id="@+id/action_search"
      android:title="@string/action_search"
      android:icon="@drawable/action_search"
      yourapp:showAsAction="always|collapseActionView"
      yourapp:actionViewClass="android.support.v7.widget.SearchView" />
</menu>

NOTE:

Application can be anything, in the above example as it is yourapp. It has to point to http://schemas.android.com/apk/res-auto schema though.

The typical example with account to your problem is shown at:

Android-nullpointerexception-on-searchview-in-action-bar.

The code is included above for clarity of the problem.

UPDATE:

To clear your mistakes in the xml look at the following code of your xml -

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

    <!-- Search / will display always -->
    <item android:id="@+id/action_search"
        android:title="@string/action_search"
        android:icon="@drawable/ic_action_search"
        android:showAsAction="ifRoom|collapseActionView"
        android:actionViewClass="android.widget.searchview"/>

Just replace the above code with the following ---

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:yourapp="http://schemas.android.com/apk/res-auto">
        <!-- Search / will display always -->
        <item android:id="@+id/action_search"
            android:title="@string/action_search"
            android:icon="@drawable/ic_action_search"
            yourapp:showAsAction="ifRoom|collapseActionView"
            yourapp:actionViewClass="android.widget.searchview"/>

Please note the changes in line 2, 7 and 8.

SOURCE: Action View Bar.

Hope this will help you!

Community
  • 1
  • 1
sjain
  • 23,126
  • 28
  • 107
  • 185
1

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).

user
  • 86,916
  • 18
  • 197
  • 190