由于java.lang.NullPointerException而崩溃

时间:2019-11-13 14:06:22

标签: java android

请帮助我解决一些问题。我不是Java专业人士,也无法理解为什么该代码会导致崩溃。我可以看到一些整数变为空,但是再次,我不明白为什么。 感谢您的关注。对不起,我的英语。

以下是崩溃日志:

  

java.lang.NullPointerException:尝试在空对象引用上调用接口方法'java.lang.Object java.util.List.get(int)'       在my.package.mpservice.MediaPlayerService $ 1.run(MediaPlayerService.java:143)

这是活动代码:

 package my.package.mpservice;

    import android.app.Service;
    import android.content.Intent;
    import android.media.AudioManager;
    import android.media.MediaPlayer;
    import android.media.MediaPlayer.OnPreparedListener;
    import android.media.MediaPlayer.OnCompletionListener;
    import android.media.MediaPlayer.OnErrorListener;
    import android.os.Binder;
    import android.os.IBinder;
    import android.os.PowerManager;
    import android.support.annotation.Nullable;

    import com.google.common.base.Preconditions;

    import org.greenrobot.eventbus.EventBus;
    import org.greenrobot.eventbus.Subscribe;
    import org.greenrobot.eventbus.ThreadMode;

    import java.io.IOException;
    import java.util.List;
    import java.util.Random;

    import my.package.constants.Constants;
    import my.package.constants.Constants.State;
    import my.package.listeners.listview.OnItemClickListener;
    import my.package.models.ITrack;


    public class MediaPlayerService extends Service implements OnPreparedListener, OnCompletionListener, OnErrorListener {

        private List<ITrack> mTracks;
        private int mPosition = -1;
        private Thread mThread;
        private final IBinder mBinder = new OnBinder();
        private MediaPlayer mPlayer;
        public State mState = State.PREPARING;
        private Boolean isRepeat = false;
        private Boolean isShuffle = false;


        public class OnBinder extends Binder {
            MediaPlayerService getService() {
                return MediaPlayerService.this;
            }
        }

        @SuppressWarnings("unused")
        @Subscribe(threadMode = ThreadMode.BACKGROUND)
        public void onEventBusGetTracks(@Nullable final List<ITrack> tracks) {
          if(tracks != null){
              mTracks = tracks;
          }
        }


        private void setUpTrackToPlay(Intent intent){
         //   if(mPosition != Integer.parseInt(intent.getStringExtra(Constants.TRACK_POSITION))){
                mPosition = Integer.parseInt(intent.getStringExtra(Constants.TRACK_POSITION));
                playTrack(mPosition);
          //  }

            if(mPosition != -1){
                // if mPosition is different to -1 that means there's a track which is playing,
                // so shows the FooterPlayerView when the user clicks on a item or the device orientation changes.
                EventBus.getDefault().post(mTracks.get(mPosition));
            }


        }


        @Override
        public void onCreate() {
            EventBus.getDefault().register(this);
        }

        @Override
        public void onDestroy() {
            EventBus.getDefault().unregister(this);
            releaseMediaPlayer();
        }


        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            if(intent != null){
                setUpTrackToPlay(intent);
            }
            return START_REDELIVER_INTENT; // Do not recreate the service if it gets killed.
        }

        @Override
        public IBinder onBind(Intent intent) { return mBinder; }

        @Override
        public void onPrepared(MediaPlayer mp) {
            mp.start();
            EventBus.getDefault().post(mTracks.get(mPosition));
            mState = State.PlAYING;
        }

        @Override
        public void onCompletion(MediaPlayer mp) {
            if (isRepeat) {
                playTrack(mPosition);
            }else if(isShuffle) {
                Random r = new Random();
                mPosition = r.nextInt(mTracks.size());
                playTrack(mPosition);
            }else{
                nextSong();
            }
        }

        @Override
        public boolean onError(MediaPlayer mp, int what, int extra) {
            return false;
        }

        private void playTrack(final int position){
            terminatePreviousThread();

            mThread = new Thread(){ // To make sure it's not doing too much work on the Main Thread.
                @Override
                public void run() {
                    super.run();
                    try {
                        createMediaPlayerIfNeeded();
                        mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                        mPlayer.setDataSource(mTracks.get(position).getUrl());
                        mPlayer.prepareAsync(); // Prepares, but does not block the UI thread.
                        EventBus.getDefault().post(Constants.FOOTER_PLAYER_VISIBLE);
                    } catch (IllegalStateException | IOException e) {
                        e.printStackTrace();
                    }
                }
            };
            mThread.start();
        }

        public ITrack getCurrentTrack(){
            if(mTracks == null){
                return (ITrack) mTracks;
            }
            return mTracks.get(mPosition);
        }

        public boolean canISaveData(){
            return (mPlayer != null);
        }

        public void nextSong(){
            try{
                if(mTracks != null && !mTracks.isEmpty()){
                    if(mPosition < mTracks.size()-1){
                        mPosition++;
                        playTrack(mPosition);
                    }else{
                        mPosition = 0;
                        playTrack(mPosition);
                    }
                }
            }catch (IndexOutOfBoundsException e){
                mPosition = 0;
                playTrack(mPosition);
            }
        }

        public void backSong(){
            if(mTracks != null && !mTracks.isEmpty()){
                if(mPosition > 0){
                    mPosition--;
                    playTrack(mPosition);
                }else{
                    mPosition = mTracks.size() -1;
                    playTrack(mPosition);
                }
            }
        }

        public void onRepeatSong(){
            if(isRepeat){
                isRepeat = false;
            }else{
                isRepeat  = true;
                isShuffle = false;
            }
        }

        public void onShuffleSong(){
            if(isShuffle){
                isShuffle = false;
            }else{
                isShuffle = true;
                isRepeat  = false;
            }
        }

        public int getCurrentPosition(){
            if(mTracks == null){
                return -1;
            }
            return mPosition;
        }


        private void terminatePreviousThread(){
            if(mThread != null && mThread.isAlive()){
                mThread.interrupt();
            }
        }

        private void createMediaPlayerIfNeeded(){
            if(mPlayer == null){
                mPlayer = new MediaPlayer();
                mPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
                mPlayer.setOnPreparedListener(this);
                mPlayer.setOnCompletionListener(this);
                mPlayer.setOnErrorListener(this);
            }else {
                mPlayer.reset();
            }
        }

        public void pause(){
            if((mPlayer != null)){
                mPlayer.pause();
                mState = State.PAUSED;
            }
        }

        public void start(){
            if((mPlayer != null)){
                mPlayer.start();
                mState = State.PlAYING;
            }
        }

        private void releaseMediaPlayer(){
            if(mPlayer != null){
                mPlayer.reset();
                mPlayer.release();
                mPlayer = null;
            }
        }

        public boolean isRepeatSong(){
            return isRepeat;
        }

        public boolean isShuffleSong(){
            return isShuffle;
        }

        public MediaPlayer mediaPlayer(){
            return Preconditions.checkNotNull(mPlayer, "MediaPlayer cannot be null");
        }
    }

0 个答案:

没有答案