致命的例外:主要;接收广播意图时出错

时间:2014-05-03 21:12:27

标签: android runtime-error broadcasting

我的代码看起来很好但是在清理完我的代码后突然我开始恢复运行时错误请帮助:

错误日志:

    05-04 02:13:13.556: E/MediaPlayer(16609): start called in state 4
05-04 02:13:13.556: E/MediaPlayer(16609): error (-38, 0)
05-04 02:13:13.556: E/MediaPlayer(16609): Error (-38,0)
05-04 02:13:13.558: D/AndroidRuntime(16609): Shutting down VM
05-04 02:13:13.558: W/dalvikvm(16609): threadid=1: thread exiting with uncaught exception (group=0x41c88d40)
05-04 02:13:13.564: E/AndroidRuntime(16609): FATAL EXCEPTION: main
05-04 02:13:13.564: E/AndroidRuntime(16609): Process: com.ayush.mymusicplayer, PID: 16609
05-04 02:13:13.564: E/AndroidRuntime(16609): java.lang.RuntimeException: Error receiving broadcast Intent { act=com.ayush.mymusicplayer.seekprogress flg=0x10 (has extras) } in com.ayush.mymusicplayer.MyMainActivity$1@42553a80
05-04 02:13:13.564: E/AndroidRuntime(16609):    at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:778)
05-04 02:13:13.564: E/AndroidRuntime(16609):    at android.os.Handler.handleCallback(Handler.java:733)
05-04 02:13:13.564: E/AndroidRuntime(16609):    at android.os.Handler.dispatchMessage(Handler.java:95)
05-04 02:13:13.564: E/AndroidRuntime(16609):    at android.os.Looper.loop(Looper.java:136)
05-04 02:13:13.564: E/AndroidRuntime(16609):    at android.app.ActivityThread.main(ActivityThread.java:5102)
05-04 02:13:13.564: E/AndroidRuntime(16609):    at java.lang.reflect.Method.invokeNative(Native Method)
05-04 02:13:13.564: E/AndroidRuntime(16609):    at java.lang.reflect.Method.invoke(Method.java:515)
05-04 02:13:13.564: E/AndroidRuntime(16609):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
05-04 02:13:13.564: E/AndroidRuntime(16609):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
05-04 02:13:13.564: E/AndroidRuntime(16609):    at dalvik.system.NativeStart.main(Native Method)
05-04 02:13:13.564: E/AndroidRuntime(16609): Caused by: java.lang.NumberFormatException: Invalid int: "null"
05-04 02:13:13.564: E/AndroidRuntime(16609):    at java.lang.Integer.invalidInt(Integer.java:137)
05-04 02:13:13.564: E/AndroidRuntime(16609):    at java.lang.Integer.parseInt(Integer.java:354)
05-04 02:13:13.564: E/AndroidRuntime(16609):    at java.lang.Integer.parseInt(Integer.java:331)
05-04 02:13:13.564: E/AndroidRuntime(16609):    at com.ayush.mymusicplayer.MyMainActivity.updateUI(MyMainActivity.java:356)
05-04 02:13:13.564: E/AndroidRuntime(16609):    at com.ayush.mymusicplayer.MyMainActivity.access$0(MyMainActivity.java:352)
05-04 02:13:13.564: E/AndroidRuntime(16609):    at com.ayush.mymusicplayer.MyMainActivity$1.onReceive(MyMainActivity.java:348)
05-04 02:13:13.564: E/AndroidRuntime(16609):    at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:768)
05-04 02:13:13.564: E/AndroidRuntime(16609):    ... 9 more
05-04 02:13:18.279: I/Process(16609): Sending signal. PID: 16609 SIG: 9

MyMainActivity :(错误似乎指向此类中的reviver - “broadcastReceiver”,它接收seekbar的信息!当我启动playSong()时出现错误

public class MyMainActivity extends Activity implements OnSeekBarChangeListener {

    private ImageButton btnPlay;
    private ImageButton btnForward;
    private ImageButton btnBackward;
    private ImageButton btnNext;
    private ImageButton btnPrevious;
    private ImageButton btnPlaylist;
    private ImageButton btnRepeat;
    private ImageButton btnShuffle;
    private TextView songTitleLabel;
    private TextView songCurrentDurationLabel;
    private TextView songTotalDurationLabel;

    private SeekBar songProgressBar;
    private int seekMax;
    private int seekProgress;
    private static int songEnded = 0;
    boolean mBroadcastIsRegistered;
    private int seekPos;

    private SongsManager songManager;
    private Utilities utils;
    private int seekForwardTime = 5000; // 5000 milliseconds
    private int seekBackwardTime = 5000; // 5000 milliseconds
    private int currentSongIndex = 0;
    private boolean isShuffle = false;
    private boolean isRepeat = false;
    private boolean startedPlaying = false;
    private boolean paused = false;

    private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
    Intent serviceIntent;

    // Broadcasting
    Intent broadcastStateIntent;
    public static final String BROADCASTING = "com.ayush.mymusicplayer.broadcasting";

    // --Set up constant ID for broadcast of seekbar position--
    public static final String BROADCAST_SEEKBAR = "com.ayush.mymusicplayer.sendseekbar";
    Intent intent;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.player);

        try {
            // All player buttons
            btnPlay = (ImageButton) findViewById(R.id.btnPlay);
            btnForward = (ImageButton) findViewById(R.id.btnForward);
            btnBackward = (ImageButton) findViewById(R.id.btnBackward);
            btnNext = (ImageButton) findViewById(R.id.btnNext);
            btnPrevious = (ImageButton) findViewById(R.id.btnPrevious);
            btnPlaylist = (ImageButton) findViewById(R.id.btnPlaylist);
            btnRepeat = (ImageButton) findViewById(R.id.btnRepeat);
            btnShuffle = (ImageButton) findViewById(R.id.btnShuffle);
            songProgressBar = (SeekBar) findViewById(R.id.songProgressBar);
            songTitleLabel = (TextView) findViewById(R.id.songTitle);
            songCurrentDurationLabel = (TextView) findViewById(R.id.songCurrentDurationLabel);
            songTotalDurationLabel = (TextView) findViewById(R.id.songTotalDurationLabel);

            songManager = new SongsManager();
            utils = new Utilities();

            // --- set up seekbar intent for broadcasting new position to
            // service ---
            intent = new Intent(BROADCAST_SEEKBAR);

            broadcastStateIntent = new Intent(BROADCASTING);

            serviceIntent = new Intent(this, myPlayService.class);

            // Getting all songs list
            songsList = songManager.getPlayList();

            // set the starting title song
            songTitleLabel.setText(songsList.get(0).get("songTitle"));


            // Listeners
            songProgressBar.setOnSeekBarChangeListener(this);

            /**
             * Play button click event
             * plays a song and changes button to pause image
             * pauses a song and changes button to play image
             * */
            btnPlay.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View arg0) {
                    // check for already playing

                    if (startedPlaying == false && paused == false) {
                        // Changing button image to pause button
                        btnPlay.setImageResource(R.drawable.btn_pause);
                        playSong(currentSongIndex);
                        startedPlaying = true;
                        paused = false;
                    } else if (startedPlaying == false && paused == true) {
                        // Changing button image to play button
                        btnPlay.setImageResource(R.drawable.btn_pause);
                        resumingBroadcast();
                        paused = false;
                        startedPlaying = true;
                    }

                    else if (startedPlaying == true && paused == false) {
                        // Changing button image to play button
                        btnPlay.setImageResource(R.drawable.btn_play);
                        pausingBroadcast();
                        paused = true;
                        startedPlaying = false;
                    }
                }

            });

            /**
             * Forward button click event Forwards song specified seconds
             * */
            btnForward.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View arg0) {
                    // check if seekForward time is lesser than song duration
                    if (seekProgress + seekForwardTime <= seekMax) {
                        // forward song
                        seekPos = seekProgress + seekForwardTime;
                        intent.putExtra("seekpos", seekPos);
                        sendBroadcast(intent);
                    } else {
                        // forward to end position
                        seekPos = seekMax;
                        intent.putExtra("seekpos", seekPos);
                        sendBroadcast(intent);
                    }
                }
            });

            /**
             * Backward button click event Backward song to specified seconds
             * */
            btnBackward.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View arg0) {
                    // check if seekBackward time is greater than 0 sec
                    if (seekProgress - seekBackwardTime >= 0) {
                        // forward song
                        seekPos = seekProgress + seekBackwardTime;
                        intent.putExtra("seekpos", seekPos);
                        sendBroadcast(intent);
                    } else {
                        // backward to starting position
                        intent.putExtra("seekpos", 0);
                        sendBroadcast(intent);
                    }
                }
            });

            /**
             * Button Click event for Play list click event Launches list
             * activity which displays list of songs
             * */
            btnPlaylist.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    Intent i = new Intent(getApplicationContext(),
                            PlayListActivity.class);
                    startActivityForResult(i, 100);
                }
            });

            /**
             * Next button click event Plays next song by taking
             * currentSongIndex + 1
             * */
            btnNext.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View arg0) {
                    if (isShuffle) {
                        // shuffle is on - play a random song
                        Random rand = new Random();
                        currentSongIndex = rand.nextInt((songsList.size() - 1) - 0 + 1) + 0;
                        playSong(currentSongIndex);
                    } else {
                        // check if next song is there or not
                        if (currentSongIndex < (songsList.size() - 1)) {
                            playSong(currentSongIndex + 1);
                            currentSongIndex = currentSongIndex + 1;
                        } else {
                            // play first song
                            playSong(0);
                            currentSongIndex = 0;
                        }
                    }
                }
            });

            /**
             * Back button click event Plays previous song by currentSongIndex -
             * 1
             * */
            btnPrevious.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View arg0) {
                    if (isShuffle) {
                        // shuffle is on - play a random song
                        Random rand = new Random();
                        currentSongIndex = rand.nextInt((songsList.size() - 1) - 0 + 1) + 0;
                        playSong(currentSongIndex);
                    } else {
                        if (currentSongIndex > 0) {
                            playSong(currentSongIndex - 1);
                            currentSongIndex = currentSongIndex - 1;
                        } else {
                            // play last song
                            playSong(songsList.size() - 1);
                            currentSongIndex = songsList.size() - 1;
                        }
                    }
                }
            });

            /**
             * Button Click event for Repeat button Enables repeat flag to true
             * */
            btnRepeat.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    if (isRepeat) {
                        isRepeat = false;
                        Toast.makeText(getApplicationContext(),
                                "Repeat is OFF", Toast.LENGTH_SHORT).show();
                        btnRepeat.setImageResource(R.drawable.btn_repeat);
                    } else {
                        // make repeat to true
                        isRepeat = true;
                        Toast.makeText(getApplicationContext(), "Repeat is ON",
                                Toast.LENGTH_SHORT).show();
                        // make shuffle to false
                        isShuffle = false;
                        btnRepeat
                                .setImageResource(R.drawable.btn_repeat_focused);
                        btnShuffle.setImageResource(R.drawable.btn_shuffle);
                    }
                }
            });

            /**
             * Button Click event for Shuffle button Enables shuffle flag to
             * true
             * */
            btnShuffle.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    if (isShuffle) {
                        isShuffle = false;
                        Toast.makeText(getApplicationContext(),
                                "Shuffle is OFF", Toast.LENGTH_SHORT).show();
                        btnShuffle.setImageResource(R.drawable.btn_shuffle);
                    } else {
                        // make repeat to true
                        isShuffle = true;
                        Toast.makeText(getApplicationContext(),
                                "Shuffle is ON", Toast.LENGTH_SHORT).show();
                        // make shuffle to false
                        isRepeat = false;
                        btnShuffle
                                .setImageResource(R.drawable.btn_shuffle_focused);
                        btnRepeat.setImageResource(R.drawable.btn_repeat);
                    }
                }
            });

        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(getApplicationContext(),
                    e.getClass().getName() + " " + e.getMessage(),
                    Toast.LENGTH_LONG).show();
        }

    }
    /**
     * Receiving song index from playlist view and play the song
     * */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 100) {
            currentSongIndex = data.getExtras().getInt("songIndex");
            // play selected song
            playSong(currentSongIndex);
        }

    }

    // -- onPause, unregister broadcast receiver. To improve, also save screen data ---
    @Override
    protected void onPause() {
        // Unregister broadcast receiver
        if (mBroadcastIsRegistered) {
            unregisterReceiver(broadcastReceiver);
            mBroadcastIsRegistered = false;
        }
        super.onPause();
    }

    // -- onResume register broadcast receiver. To improve, retrieve saved screen data ---
    @Override
    protected void onResume() {
        // Register broadcast receiver
        if (!mBroadcastIsRegistered) {
            registerReceiver(broadcastReceiver, new IntentFilter(
                    myPlayService.BROADCAST_ACTION));
            mBroadcastIsRegistered = true;
        }
        super.onResume();
    }

    // -- Broadcast Receiver to update position of seekbar from service --
    private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent serviceIntent) {
            updateUI(serviceIntent);
        }
    };

    private void updateUI(Intent serviceIntent) {
        String counter = serviceIntent.getStringExtra("counter");
        String mediamax = serviceIntent.getStringExtra("mediamax");
        String strSongEnded = serviceIntent.getStringExtra("song_ended");
        seekProgress = Integer.parseInt(counter);

        seekMax = Integer.parseInt(mediamax);
        songEnded = Integer.parseInt(strSongEnded);
        songProgressBar.setMax(seekMax);
        songProgressBar.setProgress(seekProgress);
        // Displaying Total Duration time
        songTotalDurationLabel.setText("" + utils.milliSecondsToTimer(seekMax));
        // Displaying time completed playing
        songCurrentDurationLabel.setText(""
                + utils.milliSecondsToTimer(seekProgress));

        if (songEnded == 1) {
            // check for repeat is ON or OFF
            if (isRepeat) {
                // repeat is on play same song again
                playSong(currentSongIndex);
            } else if (isShuffle) {
                // shuffle is on - play a random song
                Random rand = new Random();
                currentSongIndex = rand.nextInt((songsList.size() - 1) - 0 + 1) + 0;
                playSong(currentSongIndex);
            } else {
                // no repeat or shuffle ON - play next song
                if (currentSongIndex < (songsList.size() - 1)) {
                    playSong(currentSongIndex + 1);
                    currentSongIndex = currentSongIndex + 1;
                } else {
                    // play first song
                    playSong(0);
                    currentSongIndex = 0;
                }
            }
        }
    }

    // Send a message to Server to pause
    private void pausingBroadcast() {
        // Log.v(TAG, "BufferCompleteSent");
        broadcastStateIntent.putExtra("Broadcasting", "0");
        paused = true;
        btnPlay.setImageResource(R.drawable.btn_play);
        sendBroadcast(broadcastStateIntent);

    }

    // Send a message to Server to resume
    private void resumingBroadcast() {
        broadcastStateIntent.putExtra("Broadcasting", "1");
        paused = false;
        btnPlay.setImageResource(R.drawable.btn_pause);
        sendBroadcast(broadcastStateIntent);

    }


    // Function to play a song
    public void playSong(int songIndex) {

        stopMyPlayService();
        paused = false;
        startedPlaying = true;
        serviceIntent.putExtra("currentSongIndex", songIndex);

        // -- Register receiver for seekbar--
        registerReceiver(broadcastReceiver, new IntentFilter(
                myPlayService.BROADCAST_ACTION));
        mBroadcastIsRegistered = true;

        try {

            startService(serviceIntent);

            // Displaying Song title
            String songTitle = songsList.get(songIndex).get("songTitle");
            songTitleLabel.setText(songTitle);

            // Changing Button Image to pause image
            btnPlay.setImageResource(R.drawable.btn_pause);

            // set Progress bar values
            songProgressBar.setProgress(0);
            songProgressBar.setMax(100);

        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(getApplicationContext(),
                    e.getClass().getName() + " " + e.getMessage(),
                    Toast.LENGTH_LONG).show();
        }
    }

    private void stopMyPlayService() {

        // --Unregister broadcastReceiver for seekbar
        if (mBroadcastIsRegistered) {
            try {
                unregisterReceiver(broadcastReceiver);

                mBroadcastIsRegistered = false;
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                Toast.makeText(
                getApplicationContext(),
                e.getClass().getName() + " " + e.getMessage(),
                Toast.LENGTH_LONG).show();
            }
        }

        try {
            stopService(serviceIntent);

        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(getApplicationContext(),
                    e.getClass().getName() + " " + e.getMessage(),
                    Toast.LENGTH_LONG).show();
        }
    }

    // --- When user manually moves seekbar, broadcast new position to service
    @Override
    public void onProgressChanged(SeekBar sb, int progress, boolean fromUser) {
        // TODO Auto-generated method stub
        if (fromUser) {
            seekPos = sb.getProgress();
            intent.putExtra("seekpos", seekPos);
            sendBroadcast(intent);
        }
    }

    @Override
    public void onStartTrackingTouch(SeekBar arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStopTrackingTouch(SeekBar arg0) {
        // TODO Auto-generated method stub

    }

}

myPlayService :(正在广播消息的类)

public class myPlayService extends Service implements OnCompletionListener,
        OnPreparedListener, OnSeekCompleteListener, OnErrorListener {

    // Media Player
    private MediaPlayer mp;

    private int currentSongIndex;
    private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
    private SongsManager songManager;

    // Pausing when phone call
    private boolean isPausedInCall = false;
    private PhoneStateListener phoneStateListener;
    private TelephonyManager telephonyManager;

    // Declare headsetSwitch variable
    private int headsetSwitch = 1;

    // ---Variables for seekbar processing---
    Intent seekIntent;
    String sntSeekPos;
    int intSeekPos;
    int mediaPosition;
    int mediaMax;
    private final Handler handler = new Handler();
    private int songEnded = 0;
    public static final String BROADCAST_ACTION = "com.ayush.mymusicplayer.seekprogress";

    public void onCreate() {

        // ---Set up intent for seekbar broadcast ---
        seekIntent = new Intent(BROADCAST_ACTION);

        // Register headset receiver
        registerReceiver(headsetReceiver, new IntentFilter(
                Intent.ACTION_HEADSET_PLUG));

        songManager = new SongsManager();
        songsList = songManager.getPlayList();

        mp = new MediaPlayer();
        mp.setOnCompletionListener(this);
        mp.setOnPreparedListener(this);
        mp.setOnSeekCompleteListener(this);
        mp.setOnErrorListener(this);
        mp.reset();

    }

    public int onStartCommand(Intent intent, int flags, int startId) {

        // ---Set up receiver for State change ---
        registerReceiver(broadcastActivityReceiver, new IntentFilter(
                MyMainActivity.BROADCASTING));

        // ---Set up receiver for seekbar change ---
        registerReceiver(broadcastReceiver, new IntentFilter(
                MyMainActivity.BROADCAST_SEEKBAR));

        // Manage incoming phone calls during playback. Pause mp on incoming,
        // resume on hangup.
        // -----------------------------------------------------------------------------------
        // Get the telephony manager
        telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        phoneStateListener = new PhoneStateListener() {
            @Override
            public void onCallStateChanged(int state, String incomingNumber) {
                switch (state) {
                case TelephonyManager.CALL_STATE_OFFHOOK:
                case TelephonyManager.CALL_STATE_RINGING:
                    if (mp != null) {
                        pauseMedia();
                        isPausedInCall = true;
                    }

                    break;
                case TelephonyManager.CALL_STATE_IDLE:
                    // Phone idle. Start playing.
                    if (mp != null) {
                        if (isPausedInCall) {
                            isPausedInCall = false;
                            playMedia();
                        }

                    }
                    break;
                }

            }
        };

        // Register the listener with the telephony manager
        telephonyManager.listen(phoneStateListener,
                PhoneStateListener.LISTEN_CALL_STATE);

        // Getting song Index from MyMainActivity
        currentSongIndex = intent.getExtras().getInt("currentSongIndex");
        mp.reset();

        if (!mp.isPlaying()) {
            try {

                mp.setDataSource(songsList.get(currentSongIndex)
                        .get("songPath"));
                mp.prepareAsync();

            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
            }
        }

        // --- Set up seekbar handler ---
        setupHandler();

        return START_STICKY;

    }

    // If headset gets unplugged, stop music and service.
    private BroadcastReceiver headsetReceiver = new BroadcastReceiver() {
        private boolean headsetConnected = false;

        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            if (intent.hasExtra("state")) {
                if (headsetConnected && intent.getIntExtra("state", 0) == 0) {
                    headsetConnected = false;
                    headsetSwitch = 0;
                } else if (!headsetConnected
                        && intent.getIntExtra("state", 0) == 1) {
                    headsetConnected = true;
                    headsetSwitch = 1;
                }
            }

            switch (headsetSwitch) {
            case (0):
                pauseMedia();
                break;
            case (1):
                playMedia();
                break;
            }
        }
    };

    // ---Send seekbar info to activity----
    private void setupHandler() {
        handler.removeCallbacks(sendUpdatesToUI);
        handler.postDelayed(sendUpdatesToUI, 250); // 0.25 second
    }

    private Runnable sendUpdatesToUI = new Runnable() {
        public void run() {
            LogMediaPosition();
            handler.postDelayed(this, 250); // 0.25 seconds
        }
    };

    private void LogMediaPosition() {
        if (mp.isPlaying()) {
            mediaPosition = mp.getCurrentPosition();
            mediaMax = mp.getDuration();
            seekIntent.putExtra("counter", String.valueOf(mediaPosition));
            seekIntent.putExtra("mediamax", String.valueOf(mediaMax));
            seekIntent.putExtra("song_ended", String.valueOf(songEnded));
            sendBroadcast(seekIntent);
        }

    }

    // --Receive seekbar position if it has been changed by the user in the
    // activity
    private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            updateSeekPos(intent);
        }
    };

    // Update seek position from Activity
    public void updateSeekPos(Intent intent) {
        int seekPos = intent.getIntExtra("seekpos", 0);
        if (mp.isPlaying()) {
            handler.removeCallbacks(sendUpdatesToUI);
            mp.seekTo(seekPos);
            setupHandler();
        }
    }

    // Play Song After Complition !
    @Override
    public void onSeekComplete(MediaPlayer mp) {
        if (!mp.isPlaying()) {
            playMedia();
        }

    }

    public void onPrepared(MediaPlayer arg0) {
        // Send a message to activity to end progress dialogue
        playMedia();
    }

    // Set up broadcast receiver
    private BroadcastReceiver broadcastActivityReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent broadcastIntent) {
            String broadcastValue = broadcastIntent
                    .getStringExtra("Broadcasting");
            int broadcastIntValue = Integer.parseInt(broadcastValue);

            switch (broadcastIntValue) {
            case 0:
                pauseMedia();
                break;

            case 1:
                playMedia();
                break;
            }
        }
    };

    public void pauseMedia() {
        if (mp.isPlaying()) {
            mp.pause();
        }
    }

    public void playMedia() {
        if (!mp.isPlaying()) {
            mp.start();
        }
    }

    @Override
    public void onCompletion(MediaPlayer arg0) {
        seekIntent.putExtra("song_ended", "1");
        sendBroadcast(seekIntent);
        stopSelf();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mp != null) {
            if (mp.isPlaying()) {
                mp.stop();
            }
            mp.reset();
            mp.release();
        }
        if (phoneStateListener != null) {
            telephonyManager.listen(phoneStateListener,
                    PhoneStateListener.LISTEN_NONE);
        }
        // Stop the seekbar handler from sending updates to UI
        handler.removeCallbacks(sendUpdatesToUI);
        unregisterReceiver(headsetReceiver);
        // Unregister seekbar receiver
        unregisterReceiver(broadcastReceiver);
        unregisterReceiver(broadcastActivityReceiver);
        stopSelf();
    }

    // ---Error processing ---
    @Override
    public boolean onError(MediaPlayer mp, int what, int extra) {
        switch (what) {
        case MediaPlayer.MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK:
            Toast.makeText(this,
                    "MEDIA ERROR NOT VALID FOR PROGRESSIVE PLAYBACK " + extra,
                    Toast.LENGTH_SHORT).show();
            break;
        case MediaPlayer.MEDIA_ERROR_SERVER_DIED:
            Toast.makeText(this, "MEDIA ERROR SERVER DIED " + extra,
                    Toast.LENGTH_SHORT).show();
            break;
        case MediaPlayer.MEDIA_ERROR_UNKNOWN:
            Toast.makeText(this, "MEDIA ERROR UNKNOWN " + extra,
                    Toast.LENGTH_SHORT).show();
            break;
        }
        return false;
    }

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

}

提前致谢! :)

1 个答案:

答案 0 :(得分:0)

在错误消息中查看此部分:

 NumberFormatException: Invalid int: "null"

您似乎尝试将String“null”解析为int。这是不可能的,因此会抛出NumberFormatException。