简体   繁体   English

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

[英]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. 好的,我遇到了我的Android应用程序中无法理解的问题。 In the code below, I'm getting an error on the MediaPlayer mpWeight = MediaPlayer.create(this, R.raw.mppig) ; 在下面的代码中,我在MediaPlayer mpWeight = MediaPlayer.create(this, R.raw.mppig)错误MediaPlayer mpWeight = MediaPlayer.create(this, R.raw.mppig) ;

Holding my cursor over create says: 将鼠标悬停在create上说:

The method create(Context, int) in the type MediaPlayer is not applicable for the arguments ( new View.OnFocusChangeListener(){}, int ) MediaPlayer类型的方法create(Context, int)不适用于参数( 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); thisMediaPlayer.create(this, R.raw.mppig); corresponds to the instance of OnFocusChangeListener but you need to pass the application context. 对应于OnFocusChangeListener的实例,但您需要传递应用程序上下文。

Change your create call to 将您的创建呼叫更改为

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM