Reputation: 3
Why is my async task returning null? It was working perfectly before. What am I doing wrong?
I am a new developer, I don't have much knowledge how to fix this.
Here is my code.
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: FATAL EXCEPTION: main Process: com.example.android.top10music, PID: 11359 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.start()' on a null object reference at com.example.android.top10music.NarutoActivity$PlayAudioFileBg.onPostExecute(NarutoActivity.java:404) at com.example.android.top10music.NarutoActivity$PlayAudioFileBg.onPostExecute(NarutoActivity.java:375) at android.os.AsyncTask.finish(AsyncTask.java:632) at android.os.AsyncTask.access$600(AsyncTask.java:177) at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5753) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
Upvotes: 0
Views: 1035
Reputation: 2121
You need to be careful when accessing variables in different threads. Change the mMediaPlayer
variable to a local inside the AsyncTask
rather than accessing the parent class.
@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;
}
The reason your code doesn't work is that as soon as you try to access a variable from an inner class, the variable becomes final
and therefore, in your onExecute
you cannot assign anything to it. This causes an exception which you catch and eventually makes your code return null.
Upvotes: 0