Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Null Pointer exception when using SearchView with AppCompat

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)
like image 274
Jack Avatar asked May 01 '14 18:05

Jack


2 Answers

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! :)

like image 171
LargeGlasses Avatar answered Oct 10 '22 14:10

LargeGlasses


Maybe it's an obfuscation issue...

Try adding this to your proguard file:

-keep class android.support.v7.widget.SearchView { *; }
like image 27
Alécio Carvalho Avatar answered Oct 10 '22 16:10

Alécio Carvalho