我写了以下代码:
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()));
}
}
----------------------------编辑--------------------- ----------------
@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的每个问题。但是我仍然被困住。
最佳答案
尝试使用提供的充气机代替
getActivity().getMenuInflater().inflate(R.menu.menu_main, menu);
这句话可能像以下...
inflater.inflate(R.menu.menu_main, menu);
第二件事是覆盖Fragment的
onCreate()
并编写setHasOptionsMenu(true);
应该是这样的...@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
希望它会有所帮助。