1

我有一个SherlockListFragment在不同情况下在我的应用程序中使用的泛型。这个片段有一个选项菜单项( a 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 开发分支 - 没有崩溃......谢谢大家。

4

1 回答 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);
}
于 2013-04-15T13:08:28.547 回答