上下文不recognisied:方法不适用于参数 [英] Context not recognisied:Method not applicable for arguments

查看:275
本文介绍了上下文不recognisied:方法不适用于参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个计时器在我的Andr​​oid应用程序播放MP3文件每1.5秒(嘟嘟声)。我有以下的code和收到错误,在类型的MediaPlayer的方法来创建(背景下,INT)不适用于参数(Beep.RemindTask,INT)下面我运行功能:

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

    }
}

我不明白为什么它不适用,这些参数是一样的吗?我知道它可能是与上下文,这我不能完全肯定,但是从这里:的什么是Android中语境?我知道他们正在创造新的对象或访问共享公共资源时使用。我曾尝试 getApplicationContext()的getContext() getBaseContext(),但仍收到错误。我相信,通过蜂鸣对象进行操作所需的所有位于这一背景下。任何建议或想法?

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?

推荐答案

您类是一种非活性类,所以你不能直接在场景中获得。当您从您的活动实例提示音的一个实例,通过在constuctor活动的背景。

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

这篇关于上下文不recognisied:方法不适用于参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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