如何在Fragment中实现SearchView

时间:2015-11-20 03:46:05

标签: android android-fragments android-actionbar fragment searchview

我写了以下代码:

public class TabFragment extends Fragment {
@Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        getActivity().getMenuInflater().inflate(R.menu.menu_main, menu);

        // Associate searchable configuration with the SearchView
        SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
        Log.d("Tab", "SearchManager: " + searchManager + " : " + searchView);
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
    }
}

---------------------------- EDITED ------------------ -------------------

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
}

menu.xml中的代码

<item
        android:id="@+id/action_search"
        android:actionViewClass="android.support.v7.widget.SearchView"
        android:icon="@drawable/search_icon"
        android:title="Search"
        app:showAsAction="always|collapseActionView" />

当我运行我的代码时,它会在

处抛出NullPointerException
searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));

这意味着我在

处获得了空值
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();

例外情况是:

java.lang.NullPointerException
            at com.ushatek.pustakbazaar.fragment.TabFragment.onCreateOptionsMenu(TabFragment.java:168)
            at android.support.v4.app.Fragment.performCreateOptionsMenu(Fragment.java:2047)
            at android.support.v4.app.FragmentManagerImpl.dispatchCreateOptionsMenu(FragmentManager.java:2125)
            at android.support.v4.app.FragmentController.dispatchCreateOptionsMenu(FragmentController.java:270)
            at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:299)
            at android.support.v7.internal.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:85)
            at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:243)
            at android.support.v7.internal.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:85)
            at android.support.v7.internal.app.ToolbarActionBar.populateOptionsMenu(ToolbarActionBar.java:448)
            at android.support.v7.internal.app.ToolbarActionBar$1.run(ToolbarActionBar.java:65)
            at android.os.Handler.handleCallback(Handler.java:605)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4456)
            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:787)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
            at dalvik.system.NativeStart.main(Native Method)

我已经完成了SO的每个问题。但我仍然陷入困境。

1 个答案:

答案 0 :(得分:2)

尝试使用提供的inflater而不是

getActivity().getMenuInflater().inflate(R.menu.menu_main, menu);

这句话可能如下......

inflater.inflate(R.menu.menu_main, menu);

第二件事是覆盖片段的onCreate()并写setHasOptionsMenu(true);它应该是这样的......

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
}

希望它会有所帮助。