Can't use MediaPlayer.create in new View.OnFocusChangeListener() in Android app

梦想与她 提交于 2019-12-12 02:37:15

问题


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

回答1:


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



来源:https://stackoverflow.com/questions/5691146/cant-use-mediaplayer-create-in-new-view-onfocuschangelistener-in-android-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!