在Android应用程序无法使用MediaPlayer.create新View.OnFocusChangeListener() [英] Can't use MediaPlayer.create in new View.OnFocusChangeListener() in Android app

查看:421
本文介绍了在Android应用程序无法使用MediaPlayer.create新View.OnFocusChangeListener()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

OK,我有,我没有在我的Andr​​oid应用程序理解的问题。在下面的code,我得到的的MediaPlayer mpWeight = MediaPlayer.create(这一点,R.raw.mppig)错误;

握着我的光标移到创建说:

在键入的MediaPlayer 的方法创建(背景下,INT)是不适用的参数(新View.OnFocusChangeListener(){},INT

这是什么意思,更重要的是,我该如何解决呢?

下面是整个套路:

  TextView的电视=(的TextView)findViewById(R.id.weight);
    tv.setOnFocusChangeListener(新OnFocusChangeListener(){
      @覆盖
      公共无效onFocusChange(视图V,布尔hasFocus){
            / *丢失焦点时检查文本字段
             *具有有效的值。
             * /
            如果(!hasFocus){
                浮tempweight = Float.parseFloat(et_weight.getText()的toString());
                如果(tempweight> 200){
                    MediaPlayer的mpWeight = MediaPlayer.create(这一点,R.raw.mppig);
                    mpWeight.start();
                }
            }
      }
    });


解决方案

这个 MediaPlayer.create(这一点,R.raw。 mppig); 对应 OnFocusChangeListener 的实例,但是,你需要通过应用程序上下文

更改您创建调用

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

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

这篇关于在Android应用程序无法使用MediaPlayer.create新View.OnFocusChangeListener()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆