Can't use MediaPlayer.create in new View.OnFocusChangeListener() in Android app
OK, I'm having an issue that I don't understand in my Android app. In the code below, I'm getting an error on the MediaPlayer mpWeight = MediaPlayer.create(this, R.raw.mppig)
;
Holding my cursor over create says:
The method create(Context, int)
in the type MediaPlayer
is not applicable for the arguments (开发者_开发技巧new View.OnFocusChangeListener(){}, int
)
What does that mean, and more importantly, how do I resolve it?
Here's the whole routine:
TextView tv=(TextView)findViewById(R.id.weight);
tv.setOnFocusChangeListener(new OnFocusChangeListener(){
@Override
public void onFocusChange(View v,boolean hasFocus){
/* When focus is lost check that the text field
* has valid values.
*/
if (!hasFocus) {
float tempweight = Float.parseFloat(et_weight.getText().toString());
if(tempweight > 200){
MediaPlayer mpWeight = MediaPlayer.create(this, R.raw.mppig);
mpWeight.start();
}
}
}
});
The this
in your MediaPlayer.create(this, R.raw.mppig);
corresponds to the instance of OnFocusChangeListener
but you need to pass the application context.
Change your create call to
MediaPlayer.Create(getApplicationContext(), R.raw.mppig);
精彩评论