Sherlock List Fragment'onCreate OptionMenu'在android 2.3.3上崩溃

时间:2013-04-15 12:30:27

标签: android actionbarsherlock android-listfragment android-menu

我有一个通用的SherlockListFragment,在我的应用程序中用于不同的情况。 这个片段有一个选项菜单项(SearchView),这里是SearchView创建的代码:

    @Override
public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.all_places, menu);

    final MenuItem searchMenuItem = menu.findItem(R.id.action_search);
    searchMenuItem.setOnActionExpandListener(this);

    final SearchView searchView = (SearchView) searchMenuItem
            .getActionView();
    searchView.requestFocusFromTouch();
    searchView.setQueryHint(getString(R.string.search_query_hint));
    searchView.setOnQueryTextListener(this);

}

问题是其中一个片段实例在ViewPager中使用,当刷到它时,应用程序崩溃(仅在HTC Desire上安装android 2.3.3),但适用于ICS和JB设备。顺便说一句,当手动附加片段(没有ViewPager)时,不会发生崩溃。

以下是崩溃的堆栈跟踪:

java.lang.NullPointerException
    at android.view.View.dispatchRestoreInstanceState(View.java:6132)
    at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:1213)
    at android.view.View.restoreHierarchyState(View.java:6116)
    at com.actionbarsherlock.internal.view.menu.MenuBuilder.restoreActionViewStates(MenuBuilder.java:361)
    at com.actionbarsherlock.internal.ActionBarSherlockCompat.preparePanel(ActionBarSherlockCompat.java:503)
    at com.actionbarsherlock.internal.ActionBarSherlockCompat.dispatchInvalidateOptionsMenu(ActionBarSherlockCompat.java:272)
    at com.actionbarsherlock.app.SherlockFragmentActivity.invalidateOptionsMenu(SherlockFragmentActivity.java:150)
    at com.actionbarsherlock.app.SherlockFragmentActivity.supportInvalidateOptionsMenu(SherlockFragmentActivity.java:156)
    at android.support.v4.app.Fragment.setMenuVisibility(Fragment.java:781)
    at android.support.v4.app.FragmentStatePagerAdapter.setPrimaryItem(FragmentStatePagerAdapter.java:151)
    at android.support.v4.view.ViewPager.populate(ViewPager.java:1009)
    at android.support.v4.view.ViewPager.populate(ViewPager.java:880)
    at android.support.v4.view.ViewPager$3.run(ViewPager.java:238)
    at android.os.Handler.handleCallback(Handler.java:587)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:144)
    at android.app.ActivityThread.main(ActivityThread.java:4937)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:521)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
    at dalvik.system.NativeStart.main(Native Method)

我真的坚持这个问题......任何帮助都会很棒。

修改

切换到ActionBarSherlock dev分支 - 没有崩溃...谢谢大家。

2 个答案:

答案 0 :(得分:3)

众所周知的问题。请检查此链接: https://github.com/JakeWharton/ActionBarSherlock/issues/652

答案 1 :(得分:0)

尝试:

private editTextSearch;

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getSupportMenuInflater().inflate(R.menu.your_menu, menu);       
    menu.getItem(0).setActionView(R.layout.search); //Number of menu item
    editTextSearch = (EditText) menu.getItem(0).getActionView().findViewById(R.id.edit_search); //Your editetxt search

    return super.onCreateOptionsMenu(menu);
}