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)