我寫了一個流式音樂應用程序,我工作正常,直到我嘗試在不同的過程中運行音樂服務。以下Android開發者的service,我已經添加服務的過程中艙單喜歡這裏:如何在不同的過程中運行音樂服務
<service android:name=".MyPlayer"
android:process=":myPlayer"
android:enabled="true"/>
然後在MainActivity我調用start服務:
if(_playIntent == null) {
_playIntent = new Intent(getApplicationContext(), MyPlayer.class);
startService(_playIntent);
bindService(_playIntent, musicConnection, Context.BIND_AUTO_CREATE);
}
的bindService將來電ServiceConnection:
MyPlayer _player;
private ServiceConnection musicConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
MyPlayer.MusicBinder binder = (MyPlayer.MusicBinder)service;
_player = binder.getService();
}
@Override
public void onServiceDisconnected(ComponentName name) {
isConnection = false;
}
};
然後該應用程式停止該行後工作:
MyPlayer.MusicBinder binder = (MyPlayer.MusicBinder)service;
該應用程序顯示消息「不幸,Mx舞曲已停止」,堆棧軌跡:「致命異常:main java.lang.ClassCastException:android.os.BinderProxy不能轉換爲com.xiKy.mxMusic.MyPlayer $ MusicBinder at com.xiKy.mxMusic.MainActivity $ 3.onServiceConnected(MainActivity.java:356)at android.app.LoadedApk $ ServiceDispatcher.doConnected(LoadedApk.java:1097) at android.app.LoadedApk $ ServiceDispatcher $ RunConnection.run (LoadedApk.java:1114) at android.os.Handler.handleCallback(Handler.java:615) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper .java:137) at android.app.ActivityThread.ma in(ActivityThread.java:4823) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os .ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:789) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556) at dalvik.system.NativeStart.main(Native Method) ClassLoader.loadClass :
由Thread.getContextClassLoader()返回的類加載器可能會失敗,以承載多個應用程序的進程。您應該明確指定一個上下文類加載器。例如:Thread.setContextClassLoader(getClass()。getClassLoader());
然後,如果我刪除行:
bindService(_playIntent, musicConnection, Context.BIND_AUTO_CREATE);
的應用程序不會崩潰。如何解決這個錯誤?我很感謝任何人的幫助。
「在logcat中查找我看到錯誤」 - 我不知道這是一個錯誤。 「然後應用程序停止工作後這條線」 - 請解釋什麼「停止工作」的意思。如果您的應用程序崩潰,LogCat中將會有Java堆棧跟蹤:https://stackoverflow.com/questions/23353173/uncomfort-myapp-has-stopped-how-can-i-solve-this如果您不明白堆棧跟蹤,編輯您的問題併發布整個堆棧跟蹤。 – CommonsWare
編輯,非常感謝你CommonsWare – Xiky