gpt4 book ai didi

java - 安卓:java.lang.IllegalArgumentException:服务未注册

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:44:43 26 4
gpt4 key购买 nike

我有启动服务并绑定(bind)它的 Activity 。但是我希望即使 Activity 关闭我的服务也能运行,所以我需要解除绑定(bind)。当我尝试这样做时(调用我的函数 disconnectFromService),我总是得到这个异常。

服务已成功绑定(bind),我可以与其通信。只有在绑定(bind)时我才尝试解除绑定(bind)服务。

我想,我尝试从相同的上下文绑定(bind)和解除绑定(bind)服务 - 这个 - 调用绑定(bind)和解除绑定(bind)来自 MainActivity 函数。

主要 Activity :

public class MainActivity extends AppCompatActivity {

private boolean bounded;
private SturkoPlayerService sturkoService;

...

//Connection
...
private ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
SturkoPlayerService.LocalBinder mBinder = (SturkoPlayerService.LocalBinder) service;
sturkoService = mBinder.getService();
bound = true;
...
Log.d(DEBUG_S, "Service bind: " + (bounded && sturkoService!=null)); //Binding successful
}

@Override
public void onServiceDisconnected(ComponentName name) {
bound = false;
sturkoService = null;
}
};
...


//Binding in MainActivity code
...
Intent startingIntent = new Intent(this, SturkoPlayerService.class);
startingIntent.setAction(SturkoPlayerService.PLAYER_START);
bindService(startingIntent, connection, Context.BIND_AUTO_CREATE);
...

//Unbinding
private void disconnectFromService() {
if (sturkoService != null && bound) {
...
this.unbindService(connection);
}
}
...
}

服务:

public class SturkoPlayerService extends Service {
...
public class LocalBinder extends Binder {
public SturkoPlayerService getService() {
return SturkoPlayerService.this;
}
}
...
}

编辑 1: 日志

04-14 11:57:51.873 2890-2890 D/sturkoPlayer: START
04-14 11:57:51.873 2890-2890 D/sturkoPlayer: Service bind: true
04-14 11:58:01.406 2890-2890 W/System.err: java.lang.IllegalArgumentException: Service not registered: ....MainActivity$1@4221e2d8
04-14 11:58:01.521 2890-2890 W/System.err: at android.app.LoadedApk.forgetServiceDispatcher(LoadedApk.java:947)
04-14 11:58:01.521 2890-2890 W/System.err: at android.app.ContextImpl.unbindService(ContextImpl.java:1277)
04-14 11:58:01.522 2890-2890 W/System.err: at android.content.ContextWrapper.unbindService(ContextWrapper.java:405)
04-14 11:58:01.522 2890-2890 W/System.err: at ...MainActivity.disconnectFromService(MainActivity.java:122)
04-14 11:58:01.522 2890-2890 W/System.err: at ...MainActivity.onStop(MainActivity.java:132)
04-14 11:58:01.522 2890-2890 W/System.err: at android.app.Instrumentation.callActivityOnStop(Instrumentation.java:1210)
04-14 11:58:01.522 2890-2890 W/System.err: at android.app.Activity.performStop(Activity.java:5158)
04-14 11:58:01.523 2890-2890 W/System.err: at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3397)
04-14 11:58:01.523 2890-2890 W/System.err: at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3456)
04-14 11:58:01.525 2890-2890 W/System.err: at android.app.ActivityThread.access$1200(ActivityThread.java:149)
04-14 11:58:01.526 2890-2890 W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1353)
04-14 11:58:01.527 2890-2890 W/System.err: at android.os.Handler.dispatchMessage(Handler.java:99)
04-14 11:58:01.527 2890-2890 W/System.err: at android.os.Looper.loop(Looper.java:153)
04-14 11:58:01.527 2890-2890 W/System.err: at android.app.ActivityThread.main(ActivityThread.java:4987)
04-14 11:58:01.528 2890-2890 W/System.err: at java.lang.reflect.Method.invokeNative(Native Method)
04-14 11:58:01.528 2890-2890 W/System.err: at java.lang.reflect.Method.invoke(Method.java:511)
04-14 11:58:01.529 2890-2890 W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
04-14 11:58:01.531 2890-2890 W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
04-14 11:58:01.541 2890-2890 W/System.err: at dalvik.system.NativeStart.main(Native Method)

Edit2:在 list 中,在 Activity 中

<service
android:name=".Service.SturkoPlayerService"
android:enabled="true"
android:exported="true"></service>

解决方案:

问题是,从 finish() 和 onStop() 调用了“disconnectFromService()”函数。所以它被调用了两次,当 Activity 即将结束时。

从 onStop() 中移除调用后,不再抛出异常。

不幸的是,这个错误只是我的愚蠢。

最佳答案

解决方案

问题是,disconnectFromService() 函数是从 finish()onStop() 调用的。所以它被调用了两次,当 Activity 即将结束时。

onStop() 中移除调用后,不再抛出异常。

不幸的是,这个错误只是我的愚蠢。

关于java - 安卓:java.lang.IllegalArgumentException:服务未注册,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43409135/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com