Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7457811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T13:22:13+00:00 2026-05-29T13:22:13+00:00

When i start my app and press stop or pause the android app will

  • 0

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)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T13:22:13+00:00Added an answer on May 29, 2026 at 1:22 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an app containing three buttons , play , pause and stop .
I'm using asp.net and when I press the play button to start debugging, all
I am designing an android app that needs to play a sound every minute
So I have two views. One views loads when I start the app. Then
i create some project but when start App. How to Run this App one
Is it possible for a signed Java Applet or Web Start app to write
I'm hearing that is better to have one connection open upon app start up
Greeting, I'm a C# programmer guy. I'm planning to start developing app for iphone
what parameters should i use to start a console app minimized or hidden? is
I have created an application.in the app i start a countdown timer and save

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.