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.
Author by
oggy
Updated on July 18, 2022Comments
-
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 over 13 yearsI 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 over 13 yearsI 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 over 13 yearsWhy 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 over 13 yearsthat'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 over 13 yearsHmm, 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 over 13 yearsif 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 over 13 yearsYes, 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 about 11 yearsThanks for this answer it helped me after about a month of wondering how to do this. Thank you!!
-
Yuri Brigance about 7 yearsYou 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. TheActivityClass.this
is the correct answer in this case