-1

In a multiple tabs app,i added an expandable listview and a searchview in one of the tabs(Tab2).I had an error on getComponentName() as its not supported for fragment.So i added the getActivity().getComponentName(). There is no errors but NullpointerException when running it on this line.The app crashes. Any ideas?
Here are the code's important lines:

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);

}

and the 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>

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Orestis
  • 3
  • 1
  • 3
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Phantômaxx Nov 04 '16 at 15:33
  • can you add crash logs – Sanjeet Nov 04 '16 at 15:36
  • java.lang.NullPointerException at com.example.orestis.myapp.Tab2.onCreateView(Tab2.java:34) at android.support.v4.app.Fragment.performCreateView(Fragment.java:2080) line 34 is search.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName())); – Orestis Nov 04 '16 at 18:51

2 Answers2

5

In Fragment lifecycle getActivity() can be called after Activity is created. getActivity will be available after onActivityCreated.

You can't use getActivity in onCreateView code.

Saurabh Khare
  • 1,227
  • 13
  • 24
  • First of all thanks for the answer :) appreciated ! So if i can't use it where i have to implement getComponentName() in order to work properly ? Do i have to override onActivityCreated instead of OnCreateView? – Orestis Nov 04 '16 at 19:19
  • As your code need activity information so it will be better to move piece of code to onActivityCreated. – Saurabh Khare Nov 04 '16 at 19:25
1

yo lo resolví con este artículo https://es.stackoverflow.com/questions/108085/agregar-menu-de-accion-en-un-fragmento-android-studio Me quedó así: //En el onCreate de mi fragmento agregué

setHasOptionsMenu(true);

///En el onCreateOptionsMenu del fragmento lo reemplazé con

@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;
        }
    });


}

//Y añadí en el framento

@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);
}