尝试播放声音OnItemClick时出错

时间:2013-08-21 14:03:31

标签: android listview media-player

我正在尝试从我设置的OnItemClick播放声音ListView

我收到错误“类型create(Context, int)中的方法MediaPlayer不适用于带有”创建“下划线的参数(new AdapterView.OnItemClickListener(){}, int)”。

不是100%肯定我为什么会收到此错误,如果有人可以提供帮助,我将不胜感激。

ListView BoardList = (ListView) findViewById(R.id.BoardList);
BoardList.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

String List[] = {"Play 1", "Play 2", "Play 3", "Play 4", "Play 5", "Play 6", "Play 7", "Play 8", "Play 9" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.listcustomize, R.id.textItem, List);

BoardList.setAdapter(adapter);
BoardList.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (position == 0) {
            MediaPlayer  mPlayer = MediaPlayer.create(this, R.raw.Audio1);
            mPlayer .start();
            }
        }
    });

1 个答案:

答案 0 :(得分:5)

在MediaPlayer对象中,您应该写this而不是YourActivityName.this,然后再试一次!

MediaPlayer  mPlayer = MediaPlayer.create(YourActivityName.this, R.raw.Audio1);

发生此错误是因为您在侦听器内,并且单词this指的是侦听器本身而不是您的Activity。