我的Asynctask返回null

时间:2017-08-22 16:45:04

标签: android

为什么我的异步任务返回null?它以前工作得很好。我做错了什么?

我是一名新开发人员,我不知道如何解决这个问题。

这是我的代码。

private class PlayAudioFileBg extends AsyncTask<String, Object, MediaPlayer> {

    @Override
    protected MediaPlayer doInBackground(String... params) {
        try {
            mMediaPlayer = new MediaPlayer();
            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

            mMediaPlayer.setDataSource(params[0]);

            mMediaPlayer.prepare();

            return mMediaPlayer;
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(final MediaPlayer mediaPlayer) {
        super.onPostExecute(mediaPlayer);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            mSeekBar.setProgress(0, true);
        }else{
            mSeekBar.setProgress(0);
        }
        mediaPlayer.start();
        mSeekBar.setEnabled(true);
        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                if(repeatTimes != 0){
                    mediaPlayer.start();

                }else{
                    releaseMediaPlayer();
                }

            }
        });
  

Log Cat 08-22 09:51:04.279 11359-11359 / com.example.android.top10music   E / AndroidRuntime:致命异常:主要                                                                                   处理:com.example.android.top10music,PID:11359                                                                                   java.lang.NullPointerException:尝试调用虚方法&#39; void   android.media.MediaPlayer.start()&#39;在null对象引用上                                                                                       在   com.example.android.top10music.NarutoActivity $ PlayAudioFileBg.onPostExecute(NarutoActivity.java:404)                                                                                       在   com.example.android.top10music.NarutoActivity $ PlayAudioFileBg.onPostExecute(NarutoActivity.java:375)                                                                                       在android.os.AsyncTask.finish(AsyncTask.java:632)                                                                                       在android.os.AsyncTask.access $ 600(AsyncTask.java:177)                                                                                       在   android.os.AsyncTask $ InternalHandler.handleMessage(AsyncTask.java:645)                                                                                       在android.os.Handler.dispatchMessage(Handler.java:102)                                                                                       在android.os.Looper.loop(Looper.java:135)                                                                                       在android.app.ActivityThread.main(ActivityThread.java:5753)                                                                                       at java.lang.reflect.Method.invoke(Native Method)                                                                                       在java.lang.reflect.Method.invoke(Method.java:372)                                                                                       在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1405)                                                                                       在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)

1 个答案:

答案 0 :(得分:0)

访问不同线程中的变量时需要小心。将mMediaPlayer变量更改为AsyncTask内的本地变量,而不是访问父类。

@Override
    protected MediaPlayer doInBackground(String... params) {
        try {

            MediaPlayer mMediaPlayer = new MediaPlayer();
            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

            mMediaPlayer.setDataSource(params[0]);

            mMediaPlayer.prepare();

            return mMediaPlayer;
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }

        return null;
    }

您的代码不起作用的原因是,只要您尝试从内部类访问变量,变量就会变为final,因此,在onExecute中,您无法分配任何内容它。这会导致您捕获的异常并最终使您的代码返回null。