Weeman
Weeman

Reputation: 13

Android radio streaming app crashes when you press pause or stop at the moment you haven't clicked play

When i start my app and press stop or pause the android app will crash. It works fine if you press play first and then stop or pause. I searched on google and stackoverflow but i couldn't find much about it. I think the problem is because of a NullPointerException but since i'm new too java it doesn't tell me much about the problem

The code:

     import android.app.Activity;
     import android.media.AudioManager;
     import android.media.MediaPlayer;
     import android.net.Uri;
     import android.os.Bundle;
     import android.util.Log;
     import android.view.View;
     import android.view.View.OnClickListener;
     import android.widget.Button;

     public class myMain extends Activity implements
       MediaPlayer.OnCompletionListener, MediaPlayer.OnPreparedListener,
       MediaPlayer.OnErrorListener, MediaPlayer.OnBufferingUpdateListener, OnClickListener {


      private String TAG = getClass().getSimpleName();
      private MediaPlayer mp= null;

      private Button play;
      private Button pause;
      private Button stop;




      @Override
      public void onCreate(Bundle icicle) {

       super.onCreate(icicle);
       setContentView(R.layout.main);

       play = (Button) findViewById(R.id.play);
       pause = (Button) findViewById(R.id.pause);
       stop = (Button) findViewById(R.id.stop);




       play.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
         play();
        }
       });

       pause.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view){
         pause();
        }
       });

       stop.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view){
         stop();

        }
       });
      }


      private void play() {
       Uri myUri = Uri.parse("url");
       try {
            if (mp == null) {
             this.mp = new MediaPlayer();
            } else {
             mp.stop();
             mp.reset();
            }
        mp.setDataSource(this, myUri); // Go to Initialized state
        mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mp.setOnPreparedListener(this);
        mp.setOnBufferingUpdateListener(this);

        mp.setOnErrorListener(this);
        mp.prepareAsync();

        Log.d(TAG, "LoadClip Done");
       } catch (Throwable t) {
        Log.d(TAG, t.toString());
       }
      }

      public void onPrepared(MediaPlayer mp) {
       Log.d(TAG, "Stream is prepared");
       mp.start();
      }

      private void pause() {
           mp.pause();
          }
      private void stop() {
           mp.stop();

          }

      @Override
      public void onDestroy() {
       super.onDestroy();
       stop();

      }

      public void onCompletion(MediaPlayer mp) {
       stop();
      }

      public boolean onError(MediaPlayer mp, int what, int extra) {
       StringBuilder sb = new StringBuilder();
       sb.append("Media Player Error: ");
       switch (what) {
       case MediaPlayer.MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK:
        sb.append("Not Valid for Progressive Playback");
        break;
       case MediaPlayer.MEDIA_ERROR_SERVER_DIED:
        sb.append("Server Died");
        break;
       case MediaPlayer.MEDIA_ERROR_UNKNOWN:
        sb.append("Unknown");
        break;
       default:
        sb.append(" Non standard (");
        sb.append(what);
        sb.append(")");
       }
       sb.append(" (" + what + ") ");
       sb.append(extra);
       Log.e(TAG, sb.toString());
       return true;
      }

      public void onBufferingUpdate(MediaPlayer mp, int percent) {
       Log.d(TAG, "PlayerService onBufferingUpdate : " + percent + "%");
      }

     public void onClick(View v) {
         // TODO Auto-generated method stub

     }

         }

The errors:

      02-11 20:45:43.837: D/AndroidRuntime(338): Shutting down VM
      02-11 20:45:43.837: W/dalvikvm(338): threadid=1: thread exiting with uncaught exception (group=0x40015560)
      02-11 20:45:43.857: E/AndroidRuntime(338): FATAL EXCEPTION: main
      02-11 20:45:43.857: E/AndroidRuntime(338): java.lang.NullPointerException
      02-11 20:45:43.857: E/AndroidRuntime(338):    at           wadio.media.internetradio.myMain.stop(myMain.java:95)
      02-11 20:45:43.857: E/AndroidRuntime(338):    at           wadio.media.internetradio.myMain.access$2(myMain.java:94)
      02-11 20:45:43.857: E/AndroidRuntime(338):    at wadio.media.internetradio.myMain$3.onClick(myMain.java:55)
      02-11 20:45:43.857: E/AndroidRuntime(338):    at android.view.View.performClick(View.java:2485)
      02-11 20:45:43.857: E/AndroidRuntime(338):    at android.view.View$PerformClick.run(View.java:9080)
      02-11 20:45:43.857: E/AndroidRuntime(338):    at android.os.Handler.handleCallback(Handler.java:587)
      02-11 20:45:43.857: E/AndroidRuntime(338):    at android.os.Handler.dispatchMessage(Handler.java:92)
      02-11 20:45:43.857: E/AndroidRuntime(338):    at android.os.Looper.loop(Looper.java:123)
      02-11 20:45:43.857: E/AndroidRuntime(338):    at android.app.ActivityThread.main(ActivityThread.java:3683)
      02-11 20:45:43.857: E/AndroidRuntime(338):    at java.lang.reflect.Method.invokeNative(Native Method)
      02-11 20:45:43.857: E/AndroidRuntime(338):    at java.lang.reflect.Method.invoke(Method.java:507)
      02-11 20:45:43.857: E/AndroidRuntime(338):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
      02-11 20:45:43.857: E/AndroidRuntime(338):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
      02-11 20:45:43.857: E/AndroidRuntime(338):    at dalvik.system.NativeStart.main(Native Method)

Upvotes: 1

Views: 1532

Answers (1)

yydl
yydl

Reputation: 24464

In your stop() method you access the mp variable. However, the mp variable is null until you press play (and the play() method is called). So when you try to access the null variable a NullPointerException is thrown.

A very simple way to stop the NullPointerException is to do something like this:

private void pause() {
    if(mp!=null) mp.pause();
}

private void stop() {
    if(mp!=null) mp.stop();
}

Of course this solution doesn't account for cases where pause or stop is called twice. Take a look at the MediaPlayer documentation for more info on state management.

Upvotes: 1

Related Questions