Why does NetBeans not recognize getApplicationContext ()?

5

something very strange that happens to me, is that I try to put getApplicationContext () and it tells me that

  

"can not find symbol"

I do the corresponding import and does not give an error

import android.content.ContextWrapper;

For you to understand, the mediaplayer works for me, I can create an android mediaplayer, but I can not make that work, why? I hope someone knows thank you

The source code where this problem happens is as follows package bfhsoftware.sonidoambiental;

import android.content.Context;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.widget.Toast;

/**
 *
 * @author bfhsoftware
 */
public class ReproductorAndroid extends reproductor{
    private static MediaPlayer reproductor = null;
    OnCompletionListener escuchar = sedetienelamusica();
    public ReproductorAndroid() {
        super();
    }

    @Override
    public void empezar(String proximotema) {        
        super.empezar(proximotema);
        //
        if ((reproductor == null)){
            try {
                if (proximotema.equals("")){                    
                    System.out.println(proximotema);
                    //Environment.getExternalStorageDirectory().getPath()+
                    //Uri myUri = Uri.parse(proximotema);
                    reproductor = new MediaPlayer();                 
                    reproductor.setDataSource(getApplicationContext(),Uri.parse(proximotema)); 

                    reproductor.prepare(); // might take long! (for buffering, etc)                                        
                    reproductor.setOnCompletionListener(escuchar);
                    reproductor.start();
                    System.out.println("deberia estar reproduciendo");
                }
            } catch(Exception e ) {
                e.printStackTrace();
                System.out.println("error al reproducir");
                System.out.println(e.getMessage());        
            }
        }
        reproduciendo = (reproductor != null);
    }

    @Override
    public void parar() {
        /* Llamamos a la implementación común de parada */
        super.parar();
        /* Implementamos aquí el método en android */
    }

    @Override
    public void pausa() {
        /* Llamamos a la implementación común de pausa */
        super.pausa();
        /* Implementamos aquí el método en android */
    }

    private OnCompletionListener sedetienelamusica() {
        reiniciar();
        reproducir();
        return null;
    }
    private void reiniciar(){
        reproduciendo = false;
        if (reproductor != null)
            reproductor.stop();
        reproductor = null;
    }
    public static void displayExceptionMessage(Context context, String msg)
{
    Toast.makeText(context, msg , Toast.LENGTH_LONG).show();
}
}

on the line player.setDataSource (getApplicationContext (), Uri.parse (proximotema)); the compiler tells me that getApplicationContext is not recognized as a symbol, I have tried in netbeans and in intellij idea comunity version.  For me the code is well entered, for me the problem is the platform, is not Android where I run it, how to put this syntax and compile it, and only run if the operating system is not android?

    
asked by Bernardo Harreguy 03.02.2018 в 23:00
source

2 answers

10

Since you are not inheriting from ActionBarActivity you can not access the getActivityContext () method.

One option you have is to pass to this class (through the constructor) the context of the activity at the moment you create it. To do this, you will have to indicate that, in the constructor, you receive a Context type object.

In addition to this, you have to declare a global Context type object to your class in order to match it in the constructor and thus be able to use it in other methods of your class.

It would be done in the following way:

import android.content.Context;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.widget.Toast;

/**
 *
 * @author bfhsoftware
 */
public class ReproductorAndroid extends reproductor{
private static MediaPlayer reproductor = null;
private static Context contexto;  //Declaramos un objeto tipo Context para almacenar el que nos van a pasar por el constructor
OnCompletionListener escuchar = sedetienelamusica();
public ReproductorAndroid(Context cont) {  //Indicamos que recibimos contexto
    super();
    contexto = cont;  //Ya tenemos el contexto para utilizarlo en esta clase
}

@Override
public void empezar(String proximotema) {        
    super.empezar(proximotema);
    //
    if ((reproductor == null)){
        try {
            if (proximotema.equals("")){    
...

When creating an object you will have to do it in the following way:

ReproductorAndroid repro = new ReproductorAndroid(getActivityContext());
    
answered by 09.02.2018 / 15:10
source
2
  

Because NetBeans does not recognize getApplicationContext ()?

Actually getApplicationContext () is an Android method SDK that returns the context of the Global Single Application object of the current process.

This method could be used if Netbeans had installed the Android Plugin (as it was done in Eclipse), however these plugins are already obsolete and the officially recommended to develop Android is Android Studio .

Actually what you need is the context,

  

What to do when your class does not extend from Activity, AppCompatActivity,   etc.?

In this case the option is to send the context to instantiate the class, for this the constructor is modified:

public class ReproductorAndroid extends reproductor{

    ...
    ...

    private Context context;

    public ReproductorAndroid(Context context) {
        this.context = context;
        super();
    }

    ...

In this way you can use the context using the variable context , now to instantiate the class PlayerAndroid (the use of getApplicationContext() is more "light"):

ReproductorAndroid repro = new ReproductorAndroid(getApplicationContext());
    
answered by 13.02.2018 в 01:55