所以我试图在按下项目或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
我究竟做错了什么?
最佳答案
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();
}
}
});