在ListView中点击项目时播放声音

时间:2016-05-17 10:42:45

标签: java android audio

因此,当我按下一个项目或一行ListView时,我试图播放声音。我使用此代码:

listaMjeseci.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position == 0){
                    MediaPlayer sijecanj = MediaPlayer.create(Mjeseci.this, R.raw.section1_mjeseci_sijecanj);
                    sijecanj.start();
                }
            }
        });

并获取空指针异常。 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference

我做错了什么?

1 个答案:

答案 0 :(得分:0)

    listView.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
                        if (position == 0)
                       {
                    MediaPlayer sijecanj = MediaPlayer.create(Mjeseci.this, R.raw.section1_mjeseci_sijecanj);
                    sijecanj.start();
                }
                }
            });