Reputation: 15734
I have a SearchView
in my ActionBar
. I also have a MainFragmentActivity
which loads the search in the onCreateOptionsMenu
. As I swap Fragment
s in and out the SearchView
works fine - with one exception: There is on particular Fragment
where I load like this:
FragmentTransaction t = fm.beginTransaction();
SherlockListFragment mFrag = new RateReviewFragment();
t.add(R.id.main_frag, mFrag);
t.setCustomAnimations(R.anim.animation_leave, R.anim.animation_enter,
R.anim.animation_leave, R.anim.animation_enter);
t.show(mFrag);
t.addToBackStack(null);
t.commit();
The only difference here is I am adding it to the backstack. When I try to hit the SearchView in the ActionBar while this particular Fragment is showing, or ANY Fragment After this Fragment shows (After this Fragment closes I pop the back stack and clear it), then I get the below error. It is hard to diagnose because it doesn't show in my code.
06-17 10:03:33.668: E/AndroidRuntime(9224): FATAL EXCEPTION: main
06-17 10:03:33.668: E/AndroidRuntime(9224): java.lang.NullPointerException
06-17 10:03:33.668: E/AndroidRuntime(9224): at com.android.internal.widget.ActionBarView$ExpandedActionViewMenuPresenter.expandItemActionView(ActionBarView.java:1470)
06-17 10:03:33.668: E/AndroidRuntime(9224): at com.android.internal.view.menu.MenuBuilder.expandItemActionView(MenuBuilder.java:1233)
06-17 10:03:33.668: E/AndroidRuntime(9224): at com.android.internal.view.menu.MenuItemImpl.expandActionView(MenuItemImpl.java:620)
06-17 10:03:33.668: E/AndroidRuntime(9224): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:879)
06-17 10:03:33.668: E/AndroidRuntime(9224): at com.android.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:524)
06-17 10:03:33.668: E/AndroidRuntime(9224): at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:131)
06-17 10:03:33.668: E/AndroidRuntime(9224): at android.view.View.performClick(View.java:4102)
06-17 10:03:33.668: E/AndroidRuntime(9224): at android.view.View$PerformClick.run(View.java:17085)
06-17 10:03:33.668: E/AndroidRuntime(9224): at android.os.Handler.handleCallback(Handler.java:615)
06-17 10:03:33.668: E/AndroidRuntime(9224): at android.os.Handler.dispatchMessage(Handler.java:92)
06-17 10:03:33.668: E/AndroidRuntime(9224): at android.os.Looper.loop(Looper.java:155)
06-17 10:03:33.668: E/AndroidRuntime(9224): at android.app.ActivityThread.main(ActivityThread.java:5454)
06-17 10:03:33.668: E/AndroidRuntime(9224): at java.lang.reflect.Method.invokeNative(Native Method)
06-17 10:03:33.668: E/AndroidRuntime(9224): at java.lang.reflect.Method.invoke(Method.java:511)
06-17 10:03:33.668: E/AndroidRuntime(9224): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
06-17 10:03:33.668: E/AndroidRuntime(9224): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
06-17 10:03:33.668: E/AndroidRuntime(9224): at dalvik.system.NativeStart.main(Native Method)
This is one of the few changes I have made it since I been getting this error:
android:title="Search"
android:showAsAction="always|collapseActionView"
Also, this in the FragmentActivity
searchView.setOnQueryTextListener(new OnQueryTextListener() {
public boolean onQueryTextChange(String arg0) {
// TODO Auto-generated method stub
return false;
}
public boolean onQueryTextSubmit(String arg0) {
if (searchItem != null) {
searchItem.collapseActionView();
}
return false;
}
});
This was implemented so the SearchView
auto-closes upon submission. Note: I am using ActionBarSherlock
.
Upvotes: 5
Views: 626
Reputation: 171
I was experiencing the exact same issue. After a lot of painful trial and error I finally found out that in my case the issue was setting the icon to null:
getSupportActionBar().setIcon(null);
ActionBarSherlock cryptically crashes on attempting to show the ActionView after doing this.
I hope your case is similar.
Upvotes: 9