我的服务出了问题。事实是,当它发生故障时,它会使我的所有应用程序崩溃。 调试应用程序告诉我,在“onCreate”方法结束后崩溃(请参阅下面的代码)。它似乎甚至没有访问onHandedIntent但不确定! 我把我的代码和日志放在下面。我检查了清单,这没问题。
感谢您的帮助
package com.example.pierr.puremusictest2;
import android.app.IntentService;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.util.Log;
import java.io.IOException;
public class MusicPlayerService extends IntentService{
static MediaPlayer mPlayer;
Intent gIntent;
String url = "http://lolilol.890m.com/Musique/CapitalKings/II/Capital_Kings_-_Satellites_Official_Audio_Video-r8.aac";
public MusicPlayerService() {
super("mainThreadService");
Log.i("blablabblablba","creation that's it2");
}
public void onCreate(){
Log.i("blablabblablba","creation that's it2");
mPlayer = new MediaPlayer();
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
Log.i("blablabblablba","musicPlayer created");
}
@Override
protected void onHandleIntent(Intent argo0) {
play();
}
public void play(){
try {
Log.i("voila c'est cool","play");
mPlayer.setDataSource(url);
} catch (IOException e) {
e.printStackTrace();
Log.i("test","destroy"); }
mPlayer.start();
}
}
启动我的服务的意图:
case R.id.play:
Intent intent = new Intent(getActivity(), MusicPlayerService.class);
getActivity().startService(intent);
break;
最后是LOG:
java.lang.RuntimeException: Unable to start service com.example.pierr.puremusictest2.MusicPlayerService@419ed5d8 with Intent { cmp=com.example.pierr.puremusictest2/.MusicPlayerService }: java.lang.NullPointerException
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2719)
at android.app.ActivityThread.access$2100(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1293)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5027)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.app.IntentService.onStart(IntentService.java:116)
at android.app.IntentService.onStartCommand(IntentService.java:130)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2702)
at android.app.ActivityThread.access$2100(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1293)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5027)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
在您的服务的onCreate()
方法中,在执行任何其他操作之前致电super.onCreate()
。
请注意,使用IntentService
播放音乐是一个相当奇怪的选择:
onHandleIntent()
方法中没有任何内容需要后台主题。
IntentService
将在onHandleIntent()
返回后销毁。我忘记了MediaPlayer
是否会停止播放(一旦丢失的MediaPlayer
对象被垃圾收集),或者你是否失去了对播放的控制权。