简体   繁体   中英

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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