我SearchView
的ActionBar
. 我还有一个MainFragmentActivity
加载搜索的onCreateOptionsMenu
. 当我交换Fragment
s 时,SearchView
工作正常 - 除了一个例外:我在特定的Fragment
地方加载如下:
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();
这里唯一的区别是我将它添加到后台。当我尝试在显示此特定片段时点击 ActionBar 中的 SearchView 或此片段显示后的任何片段(此片段关闭后我弹出返回堆栈并清除它),然后我收到以下错误。很难诊断,因为它没有显示在我的代码中。
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)
这是我收到此错误后所做的少数更改之一:
android:title="Search"
android:showAsAction="always|collapseActionView"
另外,这在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;
}
});
这是实施的,因此SearchView
在提交时自动关闭。注意:我正在使用ActionBarSherlock
.