13

Today I have spent the time to migrate to the AppCompat library. I have setup everything successfully except my SearchView.

Before, it functioned properly, but now I keep getting nullPointerException when using the code

searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

I have looked everywhere to try and find a solution for my problem, but I can't find one anywhere(The ones I have tried don't work).

Any guidance would be appreciated, ty.

Here is where I am calling:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);

    MenuItem searchItem = menu.findItem(R.id.search);

    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);

    SearchView searchView = (SearchView) MenuItemCompat
            .getActionView(searchItem);

    searchView.setSearchableInfo(searchManager
            .getSearchableInfo(getComponentName()));



    return super.onCreateOptionsMenu(menu);
}

Here is menu XML file:

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

<item
    android:id="@+id/about"
    android:orderInCategory="3"
    android:showAsAction="never"
    android:title="@string/about"/>
<item
    android:id="@+id/rate"
    android:orderInCategory="2"
    android:showAsAction="never"
    android:title="@string/rate"/>
<item
    android:id="@+id/search"
    android:icon="@android:drawable/ic_menu_search"
    android:orderInCategory="1"
    app:actionViewClass="android.support.v7.widget.SearchView"
    compat:showAsAction="ifRoom|collapseActionView">
</item>

Manifest:

    <activity
        android:name="com.stack.overflow.MainActivity"
        android:label="@string/app_name"
        android:launchMode="singleTop"
        android:theme="@style/Theme.AppCompat" >
        <meta-data
            android:name="android.app.default_searchable"
            android:resource="@xml/searchable" />
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <action android:name="android.intent.action.SEARCH" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

Logcat:

FATAL EXCEPTION: main
java.lang.NullPointerException
at com.stack.overflow.MainActivity.onCreateOptionsMenu(MainActivity.java:805)
at android.app.Activity.onCreatePanelMenu(Activity.java:2476)
at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:224)
at android.support.v7.app.ActionBarActivity.superOnCreatePanelMenu(ActionBarActivity.java:232)
at android.support.v7.app.ActionBarActivityDelegateICS.onCreatePanelMenu(ActionBarActivityDelegateICS.java:146)
at android.support.v7.app.ActionBarActivity.onCreatePanelMenu(ActionBarActivity.java:199)
at android.support.v7.app.ActionBarActivityDelegateICS$WindowCallbackWrapper.onCreatePanelMenu(ActionBarActivityDelegateICS.java:293)
at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:393)
at com.android.internal.policy.impl.PhoneWindow.invalidatePanelMenu(PhoneWindow.java:747)
at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:2913)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)

2 Answers 2

40

Maybe it's an obfuscation issue...

Try adding this to your proguard file:

-keep class android.support.v7.widget.SearchView { *; }
Sign up to request clarification or add additional context in comments.

4 Comments

Yup, thanks. Can't see any official documentation suggesting how you use AppCompat with proguard...
I came to the same conclusion after half a day of trial and error. This worked for me when migration to the v21 support library.
can I add this to the proguard-project.txt file or proguard.cfg, or it doesn't matter ?
it depends on what is the proguard file name you make reference to on your build file.
17

I had to do 3 things for this to work:

  1. I went to my proguard-android.txt file found on my machine at Android/sdk/tools/proguard and I pasted the following at the bottom of the file:

-keep class android.support.v7.widget.SearchView { *; }

(then I saved the file)

  1. In my action_menu.xml file in my project found at res/menu I originally had this android:actionViewClass="android.support.v7.widget.SearchView"

but I changed it to this

app:actionViewClass="android.support.v7.widget.SearchView" (the only difference being the word android and app)

So it currently looks like this:

<item android:id="@+id/action_search"
        android:icon="@drawable/ic_action_search"
        android:title="@string/action_search"
        app:showAsAction="collapseActionView|ifRoom"
        android:orderInCategory="1"
        app:actionViewClass="android.support.v7.widget.SearchView" />
  1. Then I cleaned my project.

I ran it and it finally worked! No more null pointer exception! :)

3 Comments

You're second point solved my problem! What's the difference between android and app? Why does changing it to app allow it to run properly?
WayWay, the android namespace is used when accessing the Android SDK libraries, and the app namespace is used for customizing and backward compatibility. Check this out stackoverflow.com/questions/29732512/…
Thank you! I was pounding my head against a wall trying to figure out why no variation of ProGuard changes and importes could make it work without a NullPointer. The app namespace was the golden ticket.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.