ECM
ECM
总版主
总版主
  • 社区居民
  • 忠实会员
  • 原创写手
阅读:1567回复:0

Context not recognisied:Method not applicable for arguments

楼主#
更多 发布于:2016-05-13 08:15
I'm trying to make a timer to play an MP3 file every 1.5 seconds(a beep) in my android application. I have the following code and receive the error "The method create (context,int) in the type MediaPlayer is not applicable for the arguments (Beep.RemindTask,int)" in my run function below: package com.example.timer;

import java.util.Timer;
import java.util.TimerTask;
import android.media.AudioManager;
import android.media.MediaPlayer;

public class Beep {

 Timer timer;

    public Beep() {

        timer = new Timer();
        timer.schedule(new RemindTask(),
                   0,        //initial delay
                   1*1500);  //subsequent rate
    }

    class RemindTask extends TimerTask {



        public void run() {

            MediaPlayer mPlayer = MediaPlayer.create(this, R.raw.beep);
            mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mPlayer.start();


        }
    }

    public void main(String args[]) {

        new Beep();

    }
}
 I don't understand why it isn't applicable, the parameters being the same? I know its probably something to do with the context, which I am not entirely sure of, but from here: What is Context in Android? I know they are used when creating new objects or accessing shared common resources. I have tried getApplicationContext(),getContext() and getBaseContext() but still receive errors. I believe that everything needed by the beep object to operate is located in this context. Any suggestions or ideas?answer(解决方案):Your class is a non-activity class so you can't directly get at a context. When you instantiate an instance of Beep from your activity, pass in the activity's context in the constuctor. Add a variable to your Beep class to hold a context: private Context context;
 In the constructor, store it: public Beep(Context context) {
this.context=context;
//the rest of your constructor code...
}
 Then you can do this: MediaPlayer mPlayer = MediaPlayer.create(context, R.raw.beep);

 

 

 

 

 

 

 

异常中心网是一家专门收集整理程序员编程过程中遇到的常见异常(exception)以及各种异常问答中心的网站。异常中心网旨在,减少程序员在编码遇到异常,处理各种异常时间和痛苦,让程序员能更愉快的、快速的定位异常并查找对应的异常解决方案。异常中心网诚心打造最完美的编程社区为程序员用户服务,努力成为最好的程序员乐园程序员社区程序异常中心程序bug中心异常问答中心

 

喜欢0 评分0
游客

返回顶部