首页 > 解决方案 > RuntimeException: "无法销毁活动"; “服务未注册”

问题描述

我已经查看了有关此问题的其他帖子,但看不出我做错了什么。当我点击平板电脑上的“返回”按钮时,我得到

java.lang.RuntimeException: Unable to destroy activity {...TestActivity}: java.lang.IllegalArgumentException: Service not registered: ...TestActivity@1035a3d1

导致错误的行在unbindService()这里:

@Override
    protected void onDestroy( ) {
        super.onDestroy( );  // does putting this line first fix the "can't destroy activity; service is not registered" crash? No.
        if( serviceBound ) {
            getApplicationContext().unbindService( this ); // ('this' is ServiceConnection implementation)
            serviceBound = false;
        } else {
            if( DEBUG ) Log.d( TAG, "onDestroy() found that ThermocoupleService was not bound" );
        }
        onStopDisposables.clear();  // because apparently onStop() isn't always called
    }

IllegalArgumentException似乎是指,这this是我试图解除绑定的活动。它实现了ServiceConnection接口。

服务绑定onStart()如下:

bindThermoServiceIntent = new Intent( getApplicationContext(), ThermocoupleService.class );
if( !( serviceBound = bindService( bindThermoServiceIntent, /*ServiceConnection interface*/ this, Context.BIND_AUTO_CREATE ) ) ) 
    throw new RuntimeException( TAG + ": bindService() call in onStart() failed" );
serviceComponentName = getApplicationContext().startService( bindThermoServiceIntent );  // bind to it AND start it
if( DEBUG ) Log.d( TAG, "Service running with ComponentName " + serviceComponentName.toShortString() ); 

除了这个问题,活动和服务似乎工作正常。服务用于startForeground()保持自身运行。

标签: androidservice

解决方案


推荐阅读