Search code examples
javaandroidmedia-player

Android app force close on sd card audio load


I'm trying to load an mp3 file which is stored on my sdcard. I'm using an HTC One phone that's why the path looks funny but i'm 90% it's correct.

Could somebody help me figure out why the app keeps force closing when I start my activity page2.java?

here's my code

   public class page2 extends Activity {
    MediaPlayer mediaPlayer;

    boolean isPlaying = false;  


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.page2);

        try {

            mediaPlayer.setDataSource("/storage/emulated/0/Audio/sounds/first.mp3");
        } catch (IllegalArgumentException | SecurityException
                | IllegalStateException | IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

         mediaPlayer.start();

        final ImageView Play_button = (ImageView)findViewById(R.id.playbutton);


                Play_button.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                  if(isPlaying){
                      Play_button.setImageResource(R.drawable.stopicon);

                       isPlaying = false;

                  }else {
                      Play_button.setImageResource(R.drawable.playicon);


                       isPlaying = true;
                  }
                 }
                });

    }







}

and this is my log

05-19 21:33:54.337: I/dalvikvm-heap(23333): Grow heap (frag case) to 20.389MB for 7322452-byte allocation
05-19 21:33:54.417: I/dalvikvm-heap(23333): Grow heap (frag case) to 27.376MB for 7322452-byte allocation
05-19 21:33:54.497: I/dalvikvm-heap(23333): Grow heap (frag case) to 34.365MB for 7322452-byte allocation
05-19 21:33:55.438: I/dalvikvm-heap(23333): Grow heap (frag case) to 129.877MB for 26873872-byte allocation
05-19 21:33:56.649: I/dalvikvm-heap(23333): Grow heap (frag case) to 155.560MB for 26873872-byte allocation
05-19 21:33:56.870: I/dalvikvm-heap(23333): Grow heap (frag case) to 164.561MB for 9437200-byte allocation
05-19 21:33:56.940: W/dalvikvm(23333): threadid=1: thread exiting with uncaught exception (group=0x416a0e18)
05-19 21:33:56.950: E/AndroidRuntime(23333): FATAL EXCEPTION: main
05-19 21:33:56.950: E/AndroidRuntime(23333): Process: com.example.app, PID: 23333
05-19 21:33:56.950: E/AndroidRuntime(23333): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app/com.example.app.page2}: java.lang.NullPointerException
05-19 21:33:56.950: E/AndroidRuntime(23333):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2596)
05-19 21:33:56.950: E/AndroidRuntime(23333):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2653)
05-19 21:33:56.950: E/AndroidRuntime(23333):    at android.app.ActivityThread.access$800(ActivityThread.java:156)
05-19 21:33:56.950: E/AndroidRuntime(23333):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
05-19 21:33:56.950: E/AndroidRuntime(23333):    at android.os.Handler.dispatchMessage(Handler.java:102)
05-19 21:33:56.950: E/AndroidRuntime(23333):    at android.os.Looper.loop(Looper.java:157)
05-19 21:33:56.950: E/AndroidRuntime(23333):    at android.app.ActivityThread.main(ActivityThread.java:5872)
05-19 21:33:56.950: E/AndroidRuntime(23333):    at java.lang.reflect.Method.invokeNative(Native Method)
05-19 21:33:56.950: E/AndroidRuntime(23333):    at java.lang.reflect.Method.invoke(Method.java:515)
05-19 21:33:56.950: E/AndroidRuntime(23333):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1069)
05-19 21:33:56.950: E/AndroidRuntime(23333):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:885)
05-19 21:33:56.950: E/AndroidRuntime(23333):    at dalvik.system.NativeStart.main(Native Method)
05-19 21:33:56.950: E/AndroidRuntime(23333): Caused by: java.lang.NullPointerException
05-19 21:33:56.950: E/AndroidRuntime(23333):    at com.example.app.page2.onCreate(page2.java:31)
05-19 21:33:56.950: E/AndroidRuntime(23333):    at android.app.Activity.performCreate(Activity.java:5312)
05-19 21:33:56.950: E/AndroidRuntime(23333):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
05-19 21:33:56.950: E/AndroidRuntime(23333):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2552)
05-19 21:33:56.950: E/AndroidRuntime(23333):    ... 11 more
05-19 21:34:02.676: D/Process(23333): killProcess, pid=23333
05-19 21:34:02.676: D/Process(23333): com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException:131 java.lang.ThreadGroup.uncaughtException:693 java.lang.ThreadGroup.uncaughtException:690 

Solution

  • Initialize your var declaration before the try condition as follows:

    mediaPlayer = new MediaPlayer();  
    

    This will create a new MediaPlayer retrieved by the var previously created.