1

I followed this question and tried my hand at calling a method which is in my Fragment. I'm trying to call the method from an activity. However it's not recognizing the fragment's method. Here's my code:

XML:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/peoplefragment">

    <ListView
        android:id="@+id/searchpeople_list"
        android:layout_height="fill_parent"
        android:layout_width="match_parent"
        android:scrollbars="none"
        android:background="#fff">
    </ListView>

</RelativeLayout>

Fragment Code:

    public class SearchPeopleTab extends Fragment {

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
        {          
            View v = inflater.inflate(R.layout.activity_search_people_tab, container, false);
            View rootView = inflater.inflate(R.layout.activity_search_people_tab, container, false);
            return rootView;
        }

        public static void UpdateResults(String requestSearch)
        {
               new GetSearchResults(requestSearch).execute();
        }

class GetSearchResults extends AsyncTask<Void, Void, Void> {

        String requestSearch;

        GetSearchResults(String searchtext)
        {
            this.requestSearch = searchtext;
        }

        @Override
        protected Void doInBackground(Void... params) {
    }

Activity Code: (Calling the Fragment's method)

 private void PopulateResults() {

        FragmentManager manager = (FragmentManager) getSupportFragmentManager();
        Fragment fragment = manager.findFragmentById(R.id.peoplefragment);
        fragment.UpdateResults(requestSearch); //thats the method in the fragment. 

}

The 'UpdateResults()' part is underlined and the following message is given as an error:

Cannot resolve method UpdateResults()

Looks like it can't find the method. What am I doing wrong?

Community
  • 1
  • 1
Jay
  • 4,873
  • 7
  • 72
  • 137

2 Answers2

1
  1. Remove the keyword static from the method.

  2. And also, store the fragment in the SearchPeopleTab reference variable, that you created.

    You don't really need the line to store the FragmentManager, you can directly use getSupportFragmentManager();

    //FragmentManager fm = (FragmentManager) getSupportFragmentManager();
    SearchPeopleTab fragment = (SearchPeopleTab) getSupportFragmentManager().findFragmentById(R.id.peoplefragment);
    fragment.UpdateResults();
    

When static methods are used, they are called using the class name. When you want a method to be called on specific objects, the method should not be static.

Samrat Dutta
  • 1,727
  • 1
  • 11
  • 23
  • What if I want to send a parameter to the method as well? as in fragment.UpdateResults("hey"); – Jay Jul 26 '15 at 22:13
  • Yes, Works the same way. `fragment.UpdateResults("hey");` If this was the solution, please do mark the answer as accepted. Helps me gain reputation points. :) Thank you. – Samrat Dutta Jul 26 '15 at 22:17
  • Yes, and do change the code for the method in the fragment too. – Samrat Dutta Jul 26 '15 at 22:18
1

You need to cast the Fragment to your defined class

private void PopulateResults() {

    FragmentManager manager = (FragmentManager) getSupportFragmentManager();
    SearchPeopleTab fragment = (SearchPeopleTab)manager.findFragmentById(R.id.peoplefragment);
    fragment.UpdateResults(); //thats the method in the fragment. 

}
Andrew Fielden
  • 3,751
  • 3
  • 31
  • 47
  • What if I want to send a parameter to the method as well? as in fragment.UpdateResults("hey"); It returns a null pointer – Jay Jul 26 '15 at 22:13
  • Just declare the method to take a String parameter. Do you get the null pointer when you call UpdateResults()? If so, can you show the code for that method? – Andrew Fielden Jul 26 '15 at 22:16
  • Updated the question :) I believe it must be because I'm trying to call AsyncTask from a static method – Jay Jul 26 '15 at 22:20