尝试在空对象引用上调用虚拟方法'void android.media.MediaPlayer.stop()'

时间:2019-05-25 07:09:13

标签: java android android-mediaplayer

我在mediaplayer实例上调用stop()方法,它显示“试图在null对象引用上调用虚拟方法'void android.media.MediaPlayer.stop()'”,这意味着mediaplayer对象为null,但是当我写它像if(mediaplayer!= null)stop();它执行得很好。但是,如果对象实际上为null,则不应执行此条件。谁能解释发生了什么事?

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_hello_moon, container, false);
    Button play = v.findViewById(R.id.play);
    Button stop = v.findViewById(R.id.stop);
    play.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(mp == null) {
                mp = MediaPlayer.create(getActivity(), R.raw.one_small_step);
                mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer mp) {
                        mp.stop();
                    }
                });
                mp.start();
            }
            else {
                mp.stop();
                try {
                    mp.prepare();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                mp.start();
            }
        }
    });
    stop.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(mp != null) {
                mp.release();
                mp = null;
            }
        }
    });
    return v;
}

//this is not working
@Override
public void onDestroy() {
    super.onDestroy();
        mp.stop();
}

//this works fine
@Override
public void onDestroy() {
    super.onDestroy();
    if(mp != null)
        mp.stop();
}

1 个答案:

答案 0 :(得分:0)

请尝试这样

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_hello_moon, container, false);
        Button play = v.findViewById(R.id.play);
        Button stop = v.findViewById(R.id.stop);
        if(mp == null) {
            mp = MediaPlayer.create(getActivity(), R.raw.one_small_step);
            mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    mp.stop();
                }
            });
        }
        play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mp != null) {
                    mp.start();
                }
            }
        });
        stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mp != null) {
                    mp.release();
                }
            }
        });
        return v;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if(mp != null)
            mp.stop();
    }