android - Trouble in adding a search widget in actionbar -


i add search function in action bar following this guide still got these errors.

    07-02 13:36:05.175  21813-21813/com.example.fieldbookv2 e/androidruntime﹕ fatal exception: main     process: com.example.fieldbookv2, pid: 21813     java.lang.nullpointerexception: attempt invoke virtual method 'void android.widget.searchview.setsearchableinfo(android.app.searchableinfo)' on null object reference             @ com.example.fieldbookv2.loginactivity.oncreateoptionsmenu(loginactivity.java:1621)             @ android.app.activity.oncreatepanelmenu(activity.java:2823)             @ android.support.v4.app.fragmentactivity.oncreatepanelmenu(fragmentactivity.java:277)             @ android.support.v7.internal.view.windowcallbackwrapper.oncreatepanelmenu(windowcallbackwrapper.java:84)             @ android.support.v7.app.appcompatdelegateimplbase$appcompatwindowcallbackbase.oncreatepanelmenu(appcompatdelegateimplbase.java:273)             @ android.support.v7.app.appcompatdelegateimplv7.preparepanel(appcompatdelegateimplv7.java:1111)             @ android.support.v7.app.appcompatdelegateimplv7.doinvalidatepanelmenu(appcompatdelegateimplv7.java:1396)             @ android.support.v7.app.appcompatdelegateimplv7.access$100(appcompatdelegateimplv7.java:89)             @ android.support.v7.app.appcompatdelegateimplv7$1.run(appcompatdelegateimplv7.java:126)             @ android.os.handler.handlecallback(handler.java:739)             @ android.os.handler.dispatchmessage(handler.java:95)             @ android.os.looper.loop(looper.java:135)             @ android.app.activitythread.main(activitythread.java:5254)             @ java.lang.reflect.method.invoke(native method)             @ java.lang.reflect.method.invoke(method.java:372)             @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:903)             @ com.android.internal.os.zygoteinit.main(zygoteinit.java:698) 

this main_activity_action.xml

<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:yourapp="http://schemas.android.com/apk/res-auto"     xmlns:app="http://schemas.android.com/tools"> <item android:id="@+id/action_search"       android:title="search"       android:icon="@drawable/ic_search_white"       app:showasaction="ifroom|collapseactionview"       app:actionviewclass="android.widget.searchview"/> </menu> 

searchable.xml

<?xml version="1.0" encoding="utf-8"?> <searchable xmlns:android="http://schemas.android.com/apk/res/android"       android:label="@string/search"       android:hint="@string/enter" /> 

loginactivity.java

@override public boolean oncreateoptionsmenu(menu menu) {     menuinflater inflater = getmenuinflater();     inflater.inflate(r.menu.activity_main_actions, menu);      // associate searchable configuration searchview     searchmanager searchmanager = (searchmanager) getsystemservice(context.search_service);     searchview searchview = (searchview) menu.finditem(r.id.action_search) .getactionview();     searchview.setsearchableinfo(searchmanager.getsearchableinfo(getcomponentname()));   return super.oncreateoptionsmenu(menu); } 

and androidmanifest.xml

<activity     android:name="com.example.fieldbookv2.loginactivity"     android:label="@string/title_activity_login" >         <meta-data             android:name="android.app.default_searchable"             android:value=".searchresultsactivity" /> </activity> 

any idea why says null reference?

fixed error. main_actions_menu.xml should this

<?xml version="1.0" encoding="utf-8"?>    <menu xmlns:android="http://schemas.android.com/apk/res/android"          xmlns:app="http://schemas.android.com/apk/res-auto">         <item android:id="@+id/action_search"               android:title="search"               android:icon="@drawable/ic_search_white"               app:showasaction="ifroom|collapseactionview"               app:actionviewclass="android.widget.searchview"/>    </menu> 

remove unnecessary tags , changed "yourapp" "app since app 1 assigned.


Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -