Context inside a Runnable

28,608

Solution 1

You should also be able to get the this reference from the outer class by using MainActivity.this.

Solution 2

You should use getBaseContext. Instead, if this runnable is within an activity, you should store the context in a class variable like this:

public class MainActivity extends Activity {
    private Context context;

    public void onCreate( Bundle icicle ) {
        context = this;


        // More Code
    }

    // More code

    new Runnable(){ 
        public void run() {  
            MediaPlayer mp = MediaPlayer.create(context, R.raw.soundfile);  

            while (true) {  
                if (something)  
                    play something  
            }  
        }
    }
}

Also you shouldn't have an infinite loop like that playing a sound over and over - there should be a sleep in there in order to prevent the sound from playing over and over in a small amount of time and overlapping the same sounds with each other.

Share:
28,608
oggy
Author by

oggy

Updated on July 18, 2022

Comments

  • oggy
    oggy almost 2 years

    I try to play a sound from R.raw. inside a Thread/Runnable But I can't get this to work.

    new Runnable(){ 
      public void run() {  
    
        //this is giving me a NullPointerException, because getBaseContext is null  
        MediaPlayer mp = MediaPlayer.create( getBaseContext(), R.raw.soundfile);  
    
        while (true) {  
          if (something)  
              play something  
        }  
      }
    
    

    How can I get the real Context inside the run method? It is null no matter what I try. Or is there a better way to do this?

  • oggy
    oggy over 13 years
    I tried that. But the context inside the run() of the Runnable of the Thread is null. Even with the class variable. The class extends Activity.
  • oggy
    oggy over 13 years
    I have a handler and that extra Runnable. But if I want to access that extra Runnable I need to declare it as a class variable and so there is no Context and getBaseContext is null
  • Miguel Morales
    Miguel Morales over 13 years
    Why is there no context? You can save it in your onCreate, or right before your start your thread: final Context myContext = ...; or extend your handler intializer like YourHandler(Context c) { mGlobalContext = c } ...
  • oggy
    oggy over 13 years
    that's what I am trying to figure out. If I declare a class variable Context c; and do c = getBaseContext(); in the onCreate Method I can print it out in the onCreate Method and it gives me something. If I print context in the Runnable it gives me null no matter what
  • Miguel Morales
    Miguel Morales over 13 years
    Hmm, have you tried saving the 'this' object, like on your onCreate() { final Context MyContext = this; } .... There should be no reason MyContext would be null unless that runnable code gets reached before the variable is initialized.
  • oggy
    oggy over 13 years
    if I do: final Context c = this; in the onCreate Method then the Runnable Method can't see the context - or did I get something wrong?
  • Miguel Morales
    Miguel Morales over 13 years
    Yes, you have several options on passing the context to the runnable. Pass the context to the Runnable when it is being initialized. Seems that it's just a coding error.
  • deucalion0
    deucalion0 about 11 years
    Thanks for this answer it helped me after about a month of wondering how to do this. Thank you!!
  • Yuri Brigance
    Yuri Brigance about 7 years
    You shouldn't have to store a reference to itself within an instance. If you're going to store a reference to the activity it would be more correct to pass it to the runnable, but not store this within the activity itself. The ActivityClass.this is the correct answer in this case