无法在 Android 应用程序中的 new View.OnFocusChangeListener() 中使用 MediaPlayer.create
好的,我的 Android 应用程序遇到了一个我不明白的问题。在下面的代码中,我在 MediaPlayer mpWeight = MediaPlayer.create(this, R.raw.mppig)
; 上遇到错误
将光标放在 create 上会显示:
MediaPlayer
类型中的方法 create(Context, int)
不适用于参数 (new View.OnFocusChangeListener(){ }, int
)
这是什么意思,更重要的是,我该如何解决它?
这是整个例程:
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();
}
}
}
});
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();
}
}
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
MediaPlayer.create(this, R.raw.mppig);
中的this
对应于OnFocusChangeListener
的实例,但您需要传递应用程序上下文。将创建调用更改为
MediaPlayer.Create(getApplicationContext(), R.raw.mppig);
The
this
in yourMediaPlayer.create(this, R.raw.mppig);
corresponds to the instance ofOnFocusChangeListener
but you need to pass the application context.Change your create call to
MediaPlayer.Create(getApplicationContext(), R.raw.mppig);