android - Null Pointer in SearchView AppCompatActivity? -
i beginner , searched question here found 4-5 question did not helped me.i trying searchview in actionbar, activity appcompatactivity code below.i using toolbar in activity. , using library too. oncreatoptionmenu:
@override public boolean oncreateoptionsmenu(menu menu) { menuinflater inflater = getmenuinflater(); inflater.inflate(r.menu.menu_main, menu); searchmanager smanager = (searchmanager) getsystemservice(context.search_service); menuitem searchmenuitem = menu.finditem(r.id.action_search); searchview searchviewaction = (searchview) menuitemcompat.getactionview(searchmenuitem); searchviewaction.setsearchableinfo(smanager.getsearchableinfo(getcomponentname())); searchviewaction.seticonifiedbydefault(false); return super.oncreateoptionsmenu(menu); }
xml:
<item android:id="@+id/action_search" android:title="action_search" mainactivity:showasaction="ifroom" mainactivity:actionviewclass="android.support.v7.widget.searchview" />
and logcat:
java.lang.nullpointerexception @ algonation.com.spm.mainactivity.oncreateoptionsmenu(mainactivity.java:183) @ android.app.activity.oncreatepanelmenu(activity.java:2571) @ android.support.v4.app.fragmentactivity.oncreatepanelmenu(fragmentactivity.java:277) @ android.support.v7.internal.view.windowcallbackwrapper.oncreatepanelmenu(windowcallbackwrapper.java:84) @ android.support.v7.app.appcompatdelegateimplbase$appcompatwindowcallback.oncreatepanelmenu(appcompatdelegateimplbase.java:251) @ android.support.v7.internal.view.windowcallbackwrapper.oncreatepanelmenu(windowcallbackwrapper.java:84) @ android.support.v7.internal.app.toolbaractionbar.populateoptionsmenu(toolbaractionbar.java:449) @ android.support.v7.internal.app.toolbaractionbar$1.run(toolbaractionbar.java:66) @ android.os.handler.handlecallback(handler.java:808) @ android.os.handler.dispatchmessage(handler.java:103) @ android.os.looper.loop(looper.java:193) @ android.app.activitythread.main(activitythread.java:5292) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:515) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:824) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:640) @ dalvik.system.nativestart.main(native method)
if need whole code of mainactivity please let me know.. thank much.
you getting nullpointerexception because below line returning null.
smanager.getsearchableinfo(getcomponentname()
you need
1 - declare activity(which needs perform search) accept action_search intent, in <intent-filter> element.
2 - specify searchable configuration use, in <meta-data> element.
assuming performing search in mainactivity (which appcompatactivity want) see below example:
public class mainactivity extends appcompatactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_my_main); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.my_main, menu); menuitem searchmenuitem = menu.finditem(r.id.action_search); searchview searchviewaction = (searchview) menuitemcompat .getactionview(searchmenuitem); // searchview , set searchable configuration searchmanager searchmanager = (searchmanager) getsystemservice(context.search_service); searchableinfo searchableinfo = searchmanager .getsearchableinfo(getcomponentname()); searchviewaction.setsearchableinfo(searchableinfo); searchviewaction.seticonifiedbydefault(false); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } }
androidmanifest.xml
<application android:allowbackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > <activity android:name=".mainactivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.search" /> </intent-filter> <meta-data android:name="android.app.searchable" android:resource="@xml/searchable" /> </activity> </application>
menu/activity_main.xml
<item android:id="@+id/action_search" app:actionviewclass="android.support.v7.widget.searchview" app:showasaction="ifroom" android:title="@string/action_search"/>
res/xml/searchable.xml
<?xml version="1.0" encoding="utf-8"?> <searchable xmlns:android="http://schemas.android.com/apk/res/android" android:label="@string/app_label" android:hint="@string/search_hint" > </searchable>
hope solve problem & reference see here.
Comments
Post a Comment