Good night everybody!! I can not figure out why I grab a NullPointerException in onCreateOptionsMenu . I implement normal setOnQueryTextFocusChangeListener and here it is. This is part of someone else's code, but everything works fine there.

code:

  @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); MenuItem searchItem = menu.findItem(R.id.action_search); SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem); // Вот тут вылетаю!!! Всмысле в первой строке после этого текста. searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { } }); searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { @Override public boolean onQueryTextSubmit(String query) { return false; } @Override public boolean onQueryTextChange(String searchQuery) { adapterForListViewMainActivity.filter(searchQuery.toString().trim()); listViewMainActivity.invalidate(); return true; } }); 

stack trace:

 05-28 09:48:19.333 1407-1407/com.vitaliylevashov.finalvariant D/AndroidRuntime: Shutting down VM 05-28 09:48:19.333 1407-1407/com.vitaliylevashov.finalvariant W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xa621b908) 05-28 09:48:19.341 1407-1407/com.vitaliylevashov.finalvariant E/AndroidRuntime: FATAL EXCEPTION: main java.lang.NullPointerException at com.vitaliylevashov.finalvariant.MainActivity.onCreateOptionsMenu(MainActivity.java:698) at android.app.Activity.onCreatePanelMenu(Activity.java:2490) at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:407) at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:769) at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:201) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749) at android.view.Choreographer.doCallbacks(Choreographer.java:562) at android.view.Choreographer.doFrame(Choreographer.java:531) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735) at android.os.Handler.handleCallback(Handler.java:725) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:5041) 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:793) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) at dalvik.system.NativeStart.main(Native Method) 05-28 09:48:19.357 411-845/system_process W/ActivityManager: Force finishing activity com.vitaliylevashov.finalvariant/.MainActivity 05-28 09:48:19.825 411-845/system_process D/dalvikvm: GC_FOR_ALLOC freed 3622K, 30% free 9255K/13160K, paused 11ms, total 13ms 
  • Which line? Attach the stacktrace error. - VAndrJ
  • action_search exactly contains the SearchView? - xkor
  • Try transferring all the code from SearchView to onPrepareOptionMenu - maybe just the view did not have time to draw. - Yuriy SPb
  • And so, too, it kreshitsya .... - Levashov Vitaly
  • one
    It seems to go. I have inherited my Activity ListActivity replaced by AppCompatActivity - Vitaly Levashov

0