Android: getApplicationContext()

Asked

Viewed 1,896 times

2

I’m trying to do

Database db = new Database(this.getApplicationContext());

Where Database is a class I have with all the methods related to Database and I’m calling to fill an Expandablelist. However, I am getting error in this, the mistake is "Cannot referenced from a static context". How can I get the context so as not to get this error?

public class ExpandableList {

public static HashMap<String, List<String>> getData() {
    HashMap<String, List<String>> expandableListDetail = new HashMap<String, List<String>>();


    List<String> exemplos = new ArrayList<String>();
    Database db = new Database(this.getApplicationContext());
(...)
  • 1

    @Thiagoluizdomacoski thank you!! If you want to enter as answer q I accept!

1 answer

2


can pass via parameter:

getData(ApplicationContext appContext)

getApplicationContext() is an Activity method, so it only exists in Activity Extended Classes (Appcompatactivity, Actionbaractivity, among others)

try the following:

    public static HashMap<String, List<String>> getData(ApplicationContext appContext) {
    // conteudo do seu metodo 
Database db = new Database(appContext);
    }

Already in your Activity:

expandableList.getData(this.getApplicationContext());

I hope I’ve helped!

Greetings!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.