Search code examples
javaandroidlistadapter

Cannot make a static reference to the non-static method getListAdapter() from the type ListActivity


public class AdapterHelp extends ListActivity{

public static ListAdapter returnAdapter(){
    ListAdapter adapter = (ListAdapter) getListAdapter();
    return adapter;
}
}

Hi,I'm having problem wth this simple class. It shows me an error. Cannot make a static reference to the non-static method getListAdapter() from the type ListActivity

So can anyone please help me. Thanks.


Solution

  • For practical purposes, static methods and variables don't need an instance of the class for them to be called or used, while any other method requires an instance of the class to be used, this means that you can't call non-static methods from static methods, given that if you do so, no one can guarantee that there is going to be an instance of the class for the required method to work.

    In the other hand if you create an instance of the class that have the getListAdapter() method inside your static method, you will be able to call it. For more information about static methods and variables you can check Wikipedia