Reputation: 1131
I have Search View in the action bar as an option menu
inonCreateOptionsMenu
method I get the search view as follow
inflater.inflate(R.menu.main, menu);
SearchView searchView = (SearchView) menu.findItem(R.id.serach).getActionView();
When I try to set setOnQueryTextListener
it gives me null pointer exception.
my code is as follow
mSearchView.setOnQueryTextListener(new OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String arg0) {
return true;
}
@Override
public boolean onQueryTextChange(String arg0) {
return false;
}
});
when i remove the listener it works fine. can someone tell what I am doing wrong.
logcat log
02-11 20:04:15.574: E/AndroidRuntime(27028): FATAL EXCEPTION: main
02-11 20:04:15.574: E/AndroidRuntime(27028): java.lang.NullPointerException
02-11 20:04:15.574: E/AndroidRuntime(27028): at com.example.test.PM_Fragment.onCreateOptionsMenu(PM_Fragment.java:61)
02-11 20:04:15.574: E/AndroidRuntime(27028): at android.app.Fragment.performCreateOptionsMenu(Fragment.java:1865)
02-11 20:04:15.574: E/AndroidRuntime(27028): at android.app.FragmentManagerImpl.dispatchCreateOptionsMenu(FragmentManager.java:1919)
02-11 20:04:15.574: E/AndroidRuntime(27028): at android.app.Activity.onCreatePanelMenu(Activity.java:2552)
02-11 20:04:15.574: E/AndroidRuntime(27028): at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:479)
02-11 20:04:15.574: E/AndroidRuntime(27028): at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:845)
02-11 20:04:15.574: E/AndroidRuntime(27028): at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:273)
02-11 20:04:15.574: E/AndroidRuntime(27028): at android.os.Handler.handleCallback(Handler.java:725)
02-11 20:04:15.574: E/AndroidRuntime(27028): at android.os.Handler.dispatchMessage(Handler.java:92)
02-11 20:04:15.574: E/AndroidRuntime(27028): at android.os.Looper.loop(Looper.java:158)
02-11 20:04:15.574: E/AndroidRuntime(27028): at android.app.ActivityThread.main(ActivityThread.java:5751)
02-11 20:04:15.574: E/AndroidRuntime(27028): at java.lang.reflect.Method.invokeNative(Native Method)
02-11 20:04:15.574: E/AndroidRuntime(27028): at java.lang.reflect.Method.invoke(Method.java:511)
02-11 20:04:15.574: E/AndroidRuntime(27028): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1083)
02-11 20:04:15.574: E/AndroidRuntime(27028): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:850)
02-11 20:04:15.574: E/AndroidRuntime(27028): at dalvik.system.NativeStart.main(Native Method)
Upvotes: 1
Views: 5648
Reputation: 1131
How I could be so dumb.
i got the search view insearchView
and i am setting listener for mSearchView
. This is total my mistake
Upvotes: 1