cannot play mp3 using mediaplayer android -


i pressed button play abc.mp3 file , "unfortunately, app has stopped".

java

public class mainactivity extends appcompatactivity{   mediaplayer background_music;   protected void oncreate(bundle savedinstancestate) {   background_music.create(this,r.raw.abc); }  public void playmusic(view view) {   background_music.start(); }  } 

xml

    <button     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:text="new button"     android:id="@+id/button"     ...     android:onclick="playmusic" /> 

it plays music , don't know why error comes up. please me.

it nullpointerexception since background_music null & try call start() on null object reference.

background_music.create(this,r.raw.abc);

the above code returns mediaplayer object not saved background_music object. should initialize

background_music = background_music.create(this,r.raw.abc); 

or

background_music = mediaplayer.create(this,r.raw.abc); //since create() static 

Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -