getActivity().getComponentName() 进入 Fragment Nullpointerexception

getActivity().getComponentName() into Fragment Nullpointerexception

在一个多选项卡应用程序中,我在其中一个选项卡 (Tab2) 中添加了一个可扩展列表视图和一个搜索视图。我在 getComponentName() 上遇到错误,因为它不支持 fragment.So 我添加了 getActivity ().getComponentName().当 运行 它在此 line.The 应用程序崩溃时,没有错误但 NullpointerException。有什么想法吗?
以下是代码的重要行:

public class Tab2 extends Fragment implements SearchView.OnQueryTextListener,SearchView.OnCloseListener{
private SearchView search;
private MylistAdapter listAdapter;
private ExpandableListView myList;
private ArrayList<Continent> continentList = new ArrayList<Continent>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.tab2, container, false);
    SearchManager searchManager = (SearchManager)  getActivity().getSystemService(Context.SEARCH_SERVICE);
    search = (SearchView) getActivity().findViewById(R.id.search);
    search.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
    search.setIconifiedByDefault(false);
    search.setOnQueryTextListener(this);
    search.setOnCloseListener(this);

    displayList();
    expandAll();
    return rootView;
}



private void expandAll(){

    int count = listAdapter.getGroupCount();
    for (int i=0; i<count; i++)
    {
        myList.expandGroup(i);
    }
}

private void displayList() {

    // display the list
    loadSomeData();

    // get reference to the ExpandableListView
    myList = (ExpandableListView) getView().findViewById(R.id.expandableList);
    // create the adapter by passing your ArrayList data
    listAdapter = new MylistAdapter(getActivity(), continentList);
    // attach the adapter to the list
    myList.setAdapter(listAdapter);

}

和 activity:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<SearchView
    android:id="@+id/search"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true" />

<ExpandableListView
    android:id="@+id/expandableList"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/search" >
</ExpandableListView>

在 Fragment 生命周期中,getActivity() 可以在创建 Activity 之后调用。 getActivity 将在 onActivityCreated.

后可用

您不能在 onCreateView 代码中使用 getActivity。

我用这篇文章解决了https://es.whosebug.com/questions/108085/agregar-menu-de-accion-en-un-fragmento-android-studio 它保持这样: //在我添加的片段的 onCreate 中

setHasOptionsMenu(true);

///在片段的onCreateOptionsMenu中我用

替换了它
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_main, menu);


    // Associate searchable configuration with the SearchView
    SearchManager searchManager = (SearchManager) getContext().getSystemService(Context.SEARCH_SERVICE);
    searchView = (SearchView) menu.findItem(R.id.action_search)
            .getActionView();
    searchView.setSearchableInfo(searchManager
            .getSearchableInfo(getActivity().getComponentName()));
    searchView.setMaxWidth(Integer.MAX_VALUE);

    // listening to search query text change
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            // filter recycler view when query submitted
            mAdapter.getFilter().filter(query);
            return false;
        }

        @Override
        public boolean onQueryTextChange(String query) {
            // filter recycler view when text is changed
            mAdapter.getFilter().filter(query);
            return false;
        }
    });


}

//我在frame中添加了

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_search) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}