Problem with the context of a Toast

1

The program marks me error in the context of toast . I already tried with getApplicationContext() , with NombreActivity.this and with only this .

I have a main activity called ReaderActivity, from it I send a call to ServiceListener, which is a java class.

Inside the ServiceListener is the toast inside an OnClick. Here is the code. [Here I call the Service Listener class

    
asked by AEFA19 12.06.2018 в 23:03
source

1 answer

0

If your class extends from Activity , AppCompatActivity etc, you can use:

getApplicationContext() , NombreActivity.this or this

If your class extends from Service you can use:

this

But in this case you should send the context to the class. In this case you can modify your class ServiceListener so that when you create it, send the context from Activity , example:

ServiceListener servListener = new ServiceListener(getApplicationContext());

This way you can get the context in the listener class.

private Context context;

 //Puedes recibir en el constructor el contexto.
 public ServiceListener(Context ctx) {
        this.context = ctx;       
  }

Now the variable context would be the one you would use as context.

    
answered by 12.06.2018 / 23:32
source