简体   繁体   中英

Playing sound when tapped on item in ListView

So I'm trying to play sound when a item, or row of ListView is pressed. I'm using this code:

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

and getting null pointer exception. Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference

What am I doing wrong?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM