无法在Android应用中的新View.OnFocusChangeListener()中使用MediaPlayer.create

时间:2011-04-17 03:02:28

标签: android android-mediaplayer

好的,我的Android应用中遇到了一个我不明白的问题。在下面的代码中,我在MediaPlayer mpWeight = MediaPlayer.create(this, R.raw.mppig);

上收到错误消息

将光标放在create上:

create(Context, int)类型中的方法MediaPlayer不适用于参数(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();
                }
            }
      }          
    });

1 个答案:

答案 0 :(得分:0)

this中的MediaPlayer.create(this, R.raw.mppig);对应OnFocusChangeListener的实例,但您需要传递应用程序上下文。

将您的创建调用更改为

MediaPlayer.Create(getApplicationContext(), R.raw.mppig);

相关问题